-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP134.cpp
170 lines (159 loc) · 4.41 KB
/
P134.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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <sstream>
struct Word {
Word *next;
std::string word;
};
struct Rule {
std::vector<std::string> from;
std::string to;
Rule(std::string to, std::string a) : to(to) {
std::istringstream ss(a);
std::string sub;
while(std::getline(ss, sub, ' ')) {
from.push_back(sub);
}
//std::cerr << "Made rule: " << to << " => '" << a << "', size: " << from.size() << std::endl;
}
bool apply(Word * w) const {
while(w != NULL) {
// apply rule, return true if changed.
Word *trial = w;
bool found = true;
for(std::vector<std::string>::const_iterator it = from.begin(); it != from.end(); ++it, trial = trial->next) {
if(trial == NULL || (!(*it == "*" && trial->word[0] != '<') && trial->word != *it)) {
//std::cerr << "Testing fail: " << from.size() << ", " << trial->word << "!=" << *it << std::endl;
found = false;
break;
}
}
if(found) {
//std::cerr << "Testing ok: " << from.size() << ": " << to << std::endl;
w->word = to;
w->next = trial;
return true;
}
w = w->next;
}
return false;
}
};
bool isWovel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';// || c == 'y';
}
int main() {
std::vector<Rule> rules;
rules.push_back(Rule("<A>", "a"));
rules.push_back(Rule("<A>", "e"));
rules.push_back(Rule("<A>", "i"));
rules.push_back(Rule("<A>", "o"));
rules.push_back(Rule("<A>", "u"));
rules.push_back(Rule("<MOD>", "ga"));
rules.push_back(Rule("<MOD>", "ge"));
rules.push_back(Rule("<MOD>", "gi"));
rules.push_back(Rule("<MOD>", "go"));
rules.push_back(Rule("<MOD>", "gu"));
rules.push_back(Rule("<BA>", "ba"));
rules.push_back(Rule("<BA>", "be"));
rules.push_back(Rule("<BA>", "bi"));
rules.push_back(Rule("<BA>", "bo"));
rules.push_back(Rule("<BA>", "bu"));
rules.push_back(Rule("<DA>", "da"));
rules.push_back(Rule("<DA>", "de"));
rules.push_back(Rule("<DA>", "di"));
rules.push_back(Rule("<DA>", "do"));
rules.push_back(Rule("<DA>", "du"));
rules.push_back(Rule("<LA>", "la"));
rules.push_back(Rule("<LA>", "le"));
rules.push_back(Rule("<LA>", "li"));
rules.push_back(Rule("<LA>", "lo"));
rules.push_back(Rule("<LA>", "lu"));
// rules.push_back(Rule("<PREDA>", "*"));
rules.push_back(Rule("<predstring>", "<predstring> <A> <predstring>"));
rules.push_back(Rule("<predstring>", "<predstring> <PREDA>"));
rules.push_back(Rule("<predstring>", "<PREDA>"));
rules.push_back(Rule("<predname>", "<LA> <predstring>"));
rules.push_back(Rule("<verbpred>", "<MOD> <predstring>"));
rules.push_back(Rule("<sentence>", "<DA> <predstring>"));
rules.push_back(Rule("<sentence>", "<predname> <BA> <predstring>"));
rules.push_back(Rule("<sentence>", "<predname> <verbpred> <predname>"));
rules.push_back(Rule("<sentence>", "<predname> <verbpred>"));
while(true) {
// Read line:
Word start;
bool first = true;
Word *last;
while(true) {
std::string s;
std::cin >> s;
if(s[0] == '#')
return 0;
Word *w;
if(first) {
w = &start;
first = false;
}
else {
w = new Word;
last->next = w;
}
last = w;
const std::size_t dotAt = s.find(".");
if(dotAt != std::string::npos) {
w->word = s.substr(0, dotAt);
}
else {
w->word = s;
}
// predicate transform:
if(!isWovel(w->word[w->word.size()-1])) {
w->word = "<predname>";
}
else if(w->word.size() == 5 &&
!isWovel(w->word[0]) &&
!isWovel(w->word[3]) &&
isWovel(w->word[1]) != isWovel(w->word[2])) {
w->word = "<PREDA>";
}
if(dotAt != std::string::npos) {
break;
}
}
last->next = NULL;
// print:
/*{
Word *w = &start;
while(w != NULL) {
std::cerr << w->word << " ";
w = w->next;
}
std::cerr << std::endl;
}//*/
// Apply rules:
bool ruleApplied = true;
while(ruleApplied) {
ruleApplied = false;
for(std::vector<Rule>::const_iterator it = rules.begin(); it != rules.end(); ++it) {
if(it->apply(&start)) {
ruleApplied = true;
/* Word *w = &start;
while(w != NULL) {
std::cerr << w->word << " ";
w = w->next;
}
std::cerr << std::endl;//*/
break;
}
}
}
if(start.next == NULL && start.word == "<sentence>") {
std::cout << "Good" << std::endl;
}
else {
std::cout << "Bad!" << std::endl;
}
}
return 0;
}