-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinite-state-machine
More file actions
69 lines (63 loc) · 1.74 KB
/
Copy pathFinite-state-machine
File metadata and controls
69 lines (63 loc) · 1.74 KB
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
#include <iostream>
#include <vector>
#include <utility>
#include <map>
using namespace std;
class Machine {
public:
struct State {
bool allowed;
};
vector< map<char, size_t> > states;
vector<State> accepted;
bool IsAcceptedStr(string str) {
size_t cur_state = 0;
for(char symbol : str) {
auto iter_cur_symbol = this->states[cur_state].find(symbol);
if(iter_cur_symbol != this->states[cur_state].end()) {
cur_state = iter_cur_symbol->second;
} else {
return 0;
}
}
return this->accepted[cur_state ].allowed;
}
};
Machine InputMachine() {
int num_of_states;
cin >> num_of_states;
Machine machine;
machine.states.resize(num_of_states);
machine.accepted.resize(num_of_states);
for(size_t state = 0; state < num_of_states; state++) {
int allowed;
cin >> allowed;
if(allowed) {
machine.accepted[state].allowed = true;
} else {
machine.accepted[state].allowed = false;
}
int num_connected;
cin >> num_connected;
for(size_t co_state = 0; co_state < num_connected; co_state++) {
char symbol;
size_t to;
cin >> symbol >> to;
machine.states[state].insert({symbol, to});
}
}
return machine;
}
int main() {
Machine machine = InputMachine();
string empty_s = "";
cout << machine.IsAcceptedStr(empty_s) << endl;
int num_of_strings;
cin >> num_of_strings;
for(int str = 0; str < num_of_strings; ++str) {
std::string cur_string;
cin >> cur_string;
cout << machine.IsAcceptedStr(cur_string) << endl;
}
return 0;
}