-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1540.cc
99 lines (87 loc) · 2.57 KB
/
1540.cc
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
//Name: The Finite State Text-Processing
//Level: 2
//Category: 実装
//Note:
/*
* ひとつの状態はエッジの集合なので,ナイーブにmapで管理する.
* あとはがんばる.
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
using namespace std;
const int INPUT_ANY = 256;
struct State {
map<int, pair<string,string> > trans;
};
string& replace_bs(string &str, char c_char) {
for(string::iterator it = str.begin(); it != str.end(); ++it) {
if(*it == '\\') {
it = str.erase(it);
if(*it == 'b') *it = ' ';
else if(*it == 'n') *it = '\n';
else if(*it == 'c') *it = c_char;
else if(*it == '\\') *it = '\\';
}
}
return str;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
for(int CNT = 1; ; ++CNT) {
int N;
cin >> N;
if(!N) break;
map<string, State> states;
while(N--) {
string name;
int transnum;
cin >> name >> transnum;
while(transnum--) {
string input, next, output;
cin >> input >> next >> output;
for(string::iterator it = input.begin(); it != input.end(); ++it) {
int val = *it;
if(*it == '\\') {
++it;
if(*it == 'n') val = '\n';
else if(*it == 'b') val = ' ';
else if(*it == '\\') val = '\\';
else if(*it == 'c') val = INPUT_ANY;
}
states[name].trans[val] = make_pair(next, output);
}
}
}
cout << "Finite State Machine " << CNT << ":" << endl;
cin.ignore(100, '\n');
string curstate = "START";
string buf = "";
string::iterator it = buf.end();
while(curstate != "END") {
if(it == buf.end()) {
getline(cin, buf);
buf += "\n";
it = buf.begin();
}
State &state = states[curstate];
pair<string,string> trans;
if(state.trans.count(*it) > 0) {
trans = state.trans[*it];
}
else {
trans = state.trans[INPUT_ANY];
}
string output = trans.second;
if(output == "\\0") output = "";
replace_bs(output, *it);
cout << output;
curstate = trans.first;
++it;
}
}
return 0;
}