
 
源代码namespace NFA
{
    public class IntList : List<int>
    {
    }
    public class Digraph
    {
        public int E { get; set; }
        public int V { get; set; }
        public IntList[] Adjs { get; set; }
        public Digraph(int v)
        {
            this.V = v;
            this.E = 0;
            Adjs = new IntList[v];
            for (int i = 0; i < v; i++)
            {
                Adjs[i] = new IntList();
            }
        }
        public void AddEdge(int from, int to)
        {
            Adjs[from].Add(to);
            E++;
        }
        public IntList Adj(int index)
        {
            return Adjs[index];
        }
    }
    public class DirectedDFS
    {
        public bool[] Marked;
        public DirectedDFS(Digraph g, int s)
        {
            Marked = new bool[g.V];
            Dfs(g, 0);
        }
        public DirectedDFS(Digraph g, List<int> source)
        {
            Marked = new bool[g.V];
            source.ForEach(x =>
                {
                    if (!Marked[x])
                    {
                        Dfs(g, x);
                    }
                });
        }
        public void Dfs(Digraph g, int v)
        {
            Marked[v] = true;
            g.Adjs[v].ForEach(x =>
                {
                    if (!Marked[x])
                    {
                        Dfs(g, x);
                    }
                });
        }
    }
}
namespace NFA
{
    public class NFA
    {
        private string regex;
        //NFA的ε转换有向图
        private Digraph G;
        public NFA(string reg)
        {
            this.regex = reg;
            Stack<int> ops = new Stack<int>();
            int M = regex.Length;
            G = new Digraph(M+1);
            //循环状态
            for (int i = 0; i < M; i++)
            {
                int lp = i;
                if (regex[i] == ‘(‘ || regex[i] == ‘|‘)
                {
                    ops.Push(i);
                }
                else if (regex[i] == ‘)‘)
                {
                    int or = ops.Pop();
                    if (regex[or] == ‘|‘)
                    {
                        lp = ops.Pop();
                        G.AddEdge(lp, or + 1);
                        G.AddEdge(or, i);
                    }
                    else
                    {
                        lp = or;
                    }
                }
                if(i<M-1 && regex[i+1] == ‘*‘)
                {
                    G.AddEdge(lp,i+1);
                    G.AddEdge(i + 1, lp);
                }
                if (regex[i] == ‘(‘ || regex[i] == ‘*‘ || regex[i] == ‘)‘)
                {
                    G.AddEdge(i, i + 1);
                }
            }
        }
        public bool Recognize(string txt)
        {
            List<int> pc = new List<int>();                   
            DirectedDFS dfs = new DirectedDFS(G, 0);
            for (int i = 0; i < G.V; i++)
            {
                if (dfs.Marked[i])
                {
                    pc.Add(i);
                }
            }
            for (int i = 0; i < txt.Length; i++)
            {
                List<int> match = new List<int>();
                foreach (int v in pc)
                {
                    if (v < regex.Length)
                    {
                        if (regex[v] == txt[i] || regex[v] == ‘.‘)
                        {
                            match.Add(v + 1);
                        }
                    }
                }
                pc = new List<int>();
                dfs = new DirectedDFS(G, match);
                for (int v = 0; v < G.V; v++)
                {
                    if (dfs.Marked[v])
                    {
                        pc.Add(v);
                    }
                }                
            }
            foreach (int v in pc)
            {
                if (v == regex.Length)
                {
                    return true;
                }
            }
            return false;
        }
    }
}