dfa 识别 正则表达式 0(0|1)*101

 0 ( 0 | 1 )*101

最简DFA:

View Code
1 #include <cstdio>
2 #include <cmath>
3 #include <cstdlib>
4 #include <cstring>
5 #include <cctype>
6 #include <vector>
7 #include <map>
8 #include <queue>
9 #include <list>
10 #include <stack>
11 #include <bitset>
12 #include <set>
13 #include <fstream>
14 #include <sstream>
15 #include <iomanip>
16 #include <algorithm>
17 #include <iostream>
18
19
20  using namespace std;
21
22 FILE* fin = stdin;
23 FILE* fout = stdout;
24
25
26
27  enum STATE
28 {
29 ss,
30 sa,
31 sb,
32 sc,
33 sz
34 };
35 STATE state;
36 struct State_Trans
37 {
38 STATE s;
39 char ch;
40
41 State_Trans(STATE state = sa, char c = '\0')
42 : s(state), ch(c)
43 {}
44
45 bool operator <(const State_Trans& other) const
46 {
47 if(this->s != other.s)
48 return this->s < other.s;
49 else
50 return this->ch < other.ch;
51 }
52 };
53
54
55
56
57
58 int main()
59 {
60 map<State_Trans, STATE> transtbl;
61 transtbl.insert(make_pair(State_Trans(ss, '0'), sa));
62 transtbl.insert(make_pair(State_Trans(sa, '0'), sa));
63 transtbl.insert(make_pair(State_Trans(sa, '1'), sb));
64 transtbl.insert(make_pair(State_Trans(sb, '0'), sc));
65 transtbl.insert(make_pair(State_Trans(sb, '1'), sb));
66 transtbl.insert(make_pair(State_Trans(sc, '0'), sa));
67 transtbl.insert(make_pair(State_Trans(sc, '1'), sz));
68 transtbl.insert(make_pair(State_Trans(sz, '0'), sc));
69 transtbl.insert(make_pair(State_Trans(sz, '1'), sb));
70 string str;
71 while(cin >> str)
72 {
73 state = ss;
74 for(int i = 0; i != str.size(); ++i)
75 {
76 if(str[i] == ' ' || str[i] == '\t')
77 continue;
78 char ch = str[i];
79
80 if(!(ch == '0' || ch == '1'))
81 break;
82 if(transtbl.find(State_Trans(state, ch)) == transtbl.end())
83 break;
84 state = transtbl[State_Trans(state, ch)];
85 }
86 if(state == sz)
87 cout << "_______ math _________" << endl;
88 else
89 cout << "not math" << endl;
90 }
91
92 return 0;
93 }

原文地址:https://www.cnblogs.com/invisible/p/2031915.html