forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
118 lines (100 loc) · 3.08 KB
/
E.cpp
File metadata and controls
118 lines (100 loc) · 3.08 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
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
using namespace std;
const int MN = 2011;
int n;
unordered_map<string, int> id;
bool isAdd[MN];
string value[MN], symbol[MN];
pair<string,string> eq[MN];
pair<int,int> child[MN];
char tmp[100111], t[MN];
int f[MN][MN], lt;
void init() {
id.clear();
memset(f, -1, sizeof f);
}
// symbol u, at position start in string t
// f[u][t] = index of first position that can not match
int get(int u, int start) {
if (start == lt) return lt;
if (f[u][start] >= 0) return f[u][start];
if (isAdd[u]) {
int x = child[u].first;
int y = child[u].second;
f[u][start] = get(y, get(x, start));
}
else {
int res = start;
for(char c : value[u]) {
if (res < lt && c == t[res])
++res;
}
f[u][start] = res;
}
// DEBUG(u);
// cout << symbol[u] << ' ' << start << ' ' << f[u][start] << endl;
return f[u][start];
}
int main() {
int ntest; scanf("%d\n", &ntest);
while (ntest--) {
scanf("%d\n", &n);
init();
int cnt = 0;
FOR(i,1,n) {
gets(tmp);
isAdd[i] = false;
REP(j,strlen(tmp)) {
if (tmp[j] == '=') tmp[j] = ' ';
if (tmp[j] == '+') {
tmp[j] = ' ';
isAdd[i] = true;
}
}
if (isAdd[i]) {
string a, b, c;
istringstream ss(tmp);
ss >> a >> b >> c;
symbol[i] = a;
eq[i] = make_pair(b, c);
}
else {
string a, b;
istringstream ss(tmp);
ss >> a >> b;
symbol[i] = a;
value[i] = b;
}
id[symbol[i]] = ++cnt;
}
FOR(i,1,n) {
// DEBUG(symbol[i]);
if (isAdd[i]) {
// cout << eq[i].first << ' ' << eq[i].second << endl;
// if (id.count(eq[i].first)) DEBUG(id[eq[i].first]);
// if (id.count(eq[i].second)) DEBUG(id[eq[i].second]);
child[i].first = id[eq[i].first];
child[i].second = id[eq[i].second];
}
else {
// DEBUG(value[i]);
}
}
gets(tmp);
string start(tmp);
gets(t);
lt = strlen(t);
int res = get(id[start], 0);
if (res >= lt) puts("YES");
else puts("NO");
}
return 0;
}