-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminDFA.cpp
87 lines (78 loc) · 2.69 KB
/
minDFA.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
//
// Created by mush on 2019/12/12.
//
#include "structs.h"
#include <set>
#include <vector>
using std::set;
using std::vector;
using std::pair;
bool split(vector<set<DFA*>>& curSet,set<char>& edge);
void getDFAmin(vector<DFA*>& finalDFA,set<char>& edge,vector<set<DFA*>>& finalDFAmin){
set<DFA*> notFinal;
//终态分开放,非终态先放在一起
for (int i = 0; i < finalDFA.size(); ++i) {
//终态
if (finalDFA[i]->sym.size()!=0){
set<DFA*> temp;
temp.insert(finalDFA[i]);
finalDFAmin.push_back(temp);
}
else{
notFinal.insert(finalDFA[i]);
}
}
if (notFinal.size()!=0) finalDFAmin.push_back(notFinal);
bool res= true;
do{
res=split(finalDFAmin,edge);
}while(res);
}
bool split(vector<set<DFA*>>& curSet,set<char>& edge){
bool isSplit=false;
for (int i = 0; i < curSet.size(); ++i) {
if (curSet[i].size()==1){
continue;
}
//有多个子集
else{
DFA* standard=*curSet[i].begin();
set<DFA*> possibleSplit;
for (const char& j:edge){
int pos=-1;
for (auto& lastDFA:curSet[i]) {
pos++;
auto standardEdge=standard->edges.find(j);
auto otherEdge=lastDFA->edges.find(j);
//是否有这条边
if ((standardEdge==standard->edges.end()&&otherEdge!=lastDFA->edges.end())
||(standardEdge!=standard->edges.end()&&otherEdge==lastDFA->edges.end())){
possibleSplit.insert(lastDFA);
curSet[i].erase(lastDFA);
isSplit=true;
}
else{
//均有
if(standardEdge!=standard->edges.end()&&otherEdge!=lastDFA->edges.end()){
//但是不等价
for (int k = 0; k <curSet.size() ; ++k) {
if (curSet[k].find(standardEdge->second)!=curSet[k].end()
&&curSet[k].find(otherEdge->second)==curSet[k].end()){
isSplit=true;
possibleSplit.insert(lastDFA);
curSet[i].erase(lastDFA);
}
}
}
}
}
if (isSplit) {
curSet.push_back(possibleSplit);
break;
}
}
if (isSplit) break;
}
}
return isSplit;
}