-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
215 lines (198 loc) · 4.19 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include<bits/stdc++.h>
using namespace std;
struct edge {
char val;
int next;
};
vector<edge> v[200];
int id = 0;
string s;
//--------------------NFA-----------------------
stack<int> st, ed, str;
void connect(char ch) {
int nst, nsst;
nst = st.top();
st.pop();
nsst = st.top();
if(ch != '@')
v[nsst].push_back({ch, nst});
}
void func1() {
ed.push(++id);
str.push('(');
}
void func2() {
int nst = st.top();
int ned = ed.top();
v[nst].push_back({'^', ned});
char ch = str.top();
while(ch != '(') {
connect(ch);
str.pop();
ch = str.top();
}
str.pop();
str.push('@');
ned = ed.top();
st.push(ned);
ed.pop();
}
void func3() {
int nst = st.top();
int ned = ed.top();
v[nst].push_back({'^', ned});
char ch = str.top();
while(ch != '(' && ch != 'S') {
connect(ch);
str.pop();
ch = str.top();
}
}
void func4() {
int nst = st.top();
st.pop();
int nsst = st.top();
v[nst].push_back({'^', nsst});
v[nsst].push_back({'^', nst});
st.push(nst);
}
void func5(char c) {
str.push(c);
st.push(++id);
}
void print_nfa() {
cout << s << "NFA" <<endl;
for(int i = 0;i <= id; ++i){
for(auto it: v[i]){
cout << i << "--" << it.val << "->" << it.next << endl;
}
}
}
void ToNFA() {
str.push('S');
st.push(id);
ed.push(++id);
for(char c: s) {
if( c == '(')
func1();
else if(c == ')')
func2();
else if(c == '|')
func3();
else if(c == '*')
func4();
else
func5(c);
}
char ch = str.top();
if(st.top() != 0)
v[st.top()].push_back({'^', 1});
while(ch != 'S') {
connect(ch);
str.pop();
ch = str.top();
}
print_nfa();
}
//--------------------DFA-----------------------
vector<edge> dv[200];
int did = 0;
struct node {
bool flag;
set<int> ns;
};
unordered_map<int,node> mp;
bool vis[200];
bool svis[200];
set<char> diff;
node bfs(set<int> u, char c) {
fill(vis, vis + 200, 0);
queue<int> q;
for(auto t: u)
q.push(t);
set<int> nextv;
bool bfg = false;
while(!q.empty()) {
int tp = q.front();
q.pop();
for(auto it: v[tp]) {
if(it.val != c)
continue;
if(vis[it.next] == true)
continue;
vis[it.next] = true;
nextv.insert(it.next);
q.push(it.next);
if(it.next == 1)
bfg = true;
}
}
return node {bfg, nextv};
}
bool check(set<int> x, set<int> y) {
if((int)x.size() != (int)y.size())
return true;
set<int>::iterator ix, iy;
for(ix = x.begin(), iy = y.begin(); ix != x.end(); ix++, iy++)
if(*ix != *iy)
return true;
return false;
}
node merge(node a, node b) {
node c;
if(a.flag || b.flag)
c.flag = true;
c.ns.insert(a.ns.begin(), a.ns.end());
c.ns.insert(b.ns.begin(), b.ns.end());
return c;
}
void print_set(set<int> s) {
cout << "集合为:" << endl;
for(auto i: s)
cout << i << " " ;
cout << endl;
}
void print_dfa() {
cout << "序号集合:" << endl;
for(int i = 0; i <= did; ++i) {
cout << i << " ";
if(mp[i].flag)
cout << "是终态,";
else
cout << "不是终态,";
print_set(mp[i].ns);
}
cout << "DFA:" << endl;
for(int i = 0; i <= did; i++)
for(auto it: dv[i])
cout << i << "--" << it.val << "->" << it.next << endl;
}
void ToDFA() {
set<int> nu;
node nd;
for(int i = 0; i <= id; i++)
for(auto it: v[i])
if(it.val != '^')
diff.insert(it.val);
nu.insert(0);
nd = bfs(nu, '^'); //原初始状态的空闭包
mp[0] = nd; //新初始状态集,0为代表元素
queue<int> q;
q.push(0);
nu.clear();
svis[0] = true;
while(!q.empty()) {
int tp = q.front();
q.pop();
set<int> tpset = mp[tp].ns;
for(char c: diff) {
node cnd = bfs(tpset, c);
}
}
}
int main1()
{
s = "0(010)";
ToNFA();
return 0;
}