-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
122 lines (121 loc) · 2.88 KB
/
E.cpp
File metadata and controls
122 lines (121 loc) · 2.88 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
119
120
121
122
/*
© 2021-06-10 09:35:02 Franco1010 All Rights Reserved
*/
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<lli,lli> ii;
typedef vector<lli> vi;
#define deb(x) cout << #x": " << (x) << endl;
lli eval(string foo){
string base = "haha";
lli ans = 0;
fore(i, 0, sz(foo)){
bool yes = true;
fore(j, 0, sz(base)){
if(i + j >= sz(foo)){
yes = false;
break;
}
if(foo[i + j] != base[j]){
yes = false;
break;
}
}
if(yes) ans++;
}
return ans;
}
struct Node{
string pre, suf;
lli tot;
string full;
Node(string pre = "", string suf = "", lli tot = 0, string full = "-1"): pre(pre), suf(suf), tot(tot), full(full) {}
Node operator + (const Node& n) {
Node res = Node(pre, n.suf, tot + n.tot);
if(full != "-1" and n.full != "-1" and sz(full) + sz(n.full) < 50){
string foo = full + n.full;
res.pre = res.suf = foo;
res.full = foo;
// res.pre = "";
// res.suf = "";
// fore(i, 0, min(sz(foo), 5)) res.pre += foo[i];
// lli cnt = 0;
// for(lli i = sz(foo) - 1; i >= 0; i--){
// res.suf += foo[i];
// cnt++;
// if(cnt == 5) break;
// }
// res.full = foo;
}else{
res.full = "-1";
}
string foo = "";
lli cnt = 0;
for(lli i = sz(suf) - 1; i >= 0; i--){
foo = suf[i] + foo;
cnt++;
if(cnt == 3) break;
}
cnt = 0;
fore(i, 0, sz(n.pre)){
foo += n.pre[i];
cnt++;
if(cnt == 3) break;
}
// deb(foo);
// deb(eval(foo));
res.tot += eval(foo);
return res;
}
friend ostream& operator << (ostream& os, const Node& n) {
return os << "(" << n.pre << ' ' << n.tot << ' ' << n.suf << "): " << n.full;
}
};
int main(){ _
// freopen("file.in","r",stdin);
// freopen("file.out","w",stdout);
lli t; cin >> t;
while(t--){
// deb(eval("haha"));
map<string, Node> mp;
lli n; cin >> n;
string var;
cin.get();
while(n--){
string s; getline(cin, s);
// deb(s);
stringstream ss(s);
ss >> var;
string op; ss >> op;
if(op == ":="){
ss >> s;
mp[var] = Node(s, s, eval(s), s);
// deb(mp[var]);
}else{
string l, r;
ss >> l >> s >> r;
mp[var] = mp[l] + mp[r];
// deb(mp[var]);
// exit(0);
}
}
auto foo = mp[var];
cout << foo.tot << ENDL;
// string foo = mp[var];
// cout << ans << ENDL;
// break;
}
return 0;
}