-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecognizes.java
More file actions
37 lines (35 loc) · 701 Bytes
/
recognizes.java
File metadata and controls
37 lines (35 loc) · 701 Bytes
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
/**
使用NFA模拟的模式匹配
*/
public boolean recognizes(String txt){
Bag<Integer> pc = new Bag<Integer>();
DirectedDFS dfs = new DirectedDFS(g, 0); // g? 起点可达
for (int v = 0; v < g.V(); v++){
if (dfs.marked(v)){
pc.add(v);
}
}
for (int i = 0; i < txt.length(); i++){
Bag<Integer> match = new Bag<Integer>();
for (int v : pc){
if (v < M){
if (re[v] == txt.charAt(i) || re[v] == '.'){
match.add(v+1);
}
}
}
pc = new Bag<Integer>();
dfs = new DirectedDFS(g, match); // 多点可达
for (int v = 0; v < G.V(); v++){
if (dfs.marked(v)){
pc.add(v);
}
}
}
for (int v : pc){
if (v == M){
return true;
}
}
return false;
}