-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathH.cpp
239 lines (192 loc) · 4.58 KB
/
H.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// https://codeforces.com/gym/105053/problem/H
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef vector<ull> vu;
typedef long long ll;
typedef vector<ll> vi;
#define fore(i, a, b) for (ull i = a, gmat = b; i < gmat; i++)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define ALL(x) x.begin(), x.end()
#define SZ(x) ((ll)(x).size())
/** Author: chilli
* License: CC0
* Description: z[x] computes the length of the longest common prefix of s[i:] and s,
* except z[0] = 0. (abacaba -> 0010301)
* Time: O(n)
* Status: stress-tested
*/
vi Z(const string& S) {
vi z(SZ(S));
ll l = 0, r = 0;
rep(i,1,SZ(S)) {
z[i] = min(max(r - i, 0ll), z[i - l]);
while (i + z[i] < SZ(S) && S[i + z[i]] == S[z[i]])
z[i]++, l = i, r = i + z[i];
}
return z;
}
string bestRoot(const string& S) {
ull n = S.size();
vi z = Z(S);
fore(i, 1, n) {
if (n % i == 0) {
bool isRoot = true;
for (ull j = i; j < n; j += i) {
if (z[j] < i) {
isRoot = false;
break;
}
}
if (isRoot) {
return S.substr(0, i);
}
}
}
return S;
}
/** Author: Simon Lindholm
* Date: 2015-03-15
* License: CC0
* Source: own work
* Description: Self-explanatory methods for string hashing.
* Status: stress-tested
*/
// Arithmetic mod 2^64-1. 2x slower than mod 2^64 and more
// code, but works on evil test data (e.g. Thue-Morse, where
// ABBA... and BAAB... of length 2^10 hash the same mod 2^64).
// "typedef ull H;" instead if you think test data is random,
// or work mod 10^9+7 if the Birthday paradox is not a problem.
struct H {
ull x; H(ull x=0) : x(x) {}
H operator+(H o) { return x + o.x + (x + o.x < x); }
H operator-(H o) { return *this + ~o.x; }
H operator*(H o) { auto m = (__uint128_t)x * o.x;
return H((ull)m) + (ull)(m >> 64); }
ull get() const { return x + !~x; }
bool operator==(H o) const { return get() == o.get(); }
bool operator<(H o) const { return get() < o.get(); }
};
static const H C = (ll)1e11+3; // (order ~ 3e9; random also ok)
struct HashInterval {
vector<H> ha, pw;
HashInterval(const string& str) : ha(SZ(str)+1), pw(ha) {
pw[0] = 1;
rep(i,0,SZ(str))
ha[i+1] = ha[i] * C + str[i],
pw[i+1] = pw[i] * C;
}
H hashInterval(ll a, ll b) { // hash [a, b)
return ha[b] - ha[a] * pw[b - a];
}
};
H hashString(const string& s){H h{}; for(char c:s) h=h*C+c;return h;}
optional<ull> findRotation(const string& S, const string& T) {
ull n = S.size();
if (T.size() != n) {
return {};
}
HashInterval SS(S + S);
H h = hashString(T);
fore(i, 0, n) {
if (SS.hashInterval(i, i + n) == h) {
return i;
}
}
return {};
}
struct oper {
bool I;
ull R;
oper(bool I = false, ull R = 0) : I(I), R(R) {}
oper comb(ull N, oper o) {
if (!o.I) {
return {I, (R + o.R) % N};
} else {
return {!I, (N - R + o.R) % N};
}
}
oper inv(ull N, oper o) {
// ans.comb(o) == *this
if (o.I) {
return {!I, (N - R + o.R) % N};
} else {
assert(false);
}
}
bool operator<(oper o) const {
return I < o.I || (I == o.I && R < o.R);
}
bool operator==(oper o) const {
return I == o.I && R == o.R;
}
};
/*
-- derivation of inv(N, o)
ans.comb(o) == *this
Assuming o.I:
{!ans.I, (N - ans.R + o.R) % N} = *this
ans.I = !I
(N - ans.R + o.R) % N = R
N - ans.R = (R - o.R + N) % N
ans.R = (N - R + o.R) % N
*/
optional<oper> nonTrivialFixed(const string& S) {
string T = S;
reverse(ALL(T));
optional<ull> i = findRotation(S, T);
if (!i.has_value()) {
return {};
} else {
return oper(true, *i);
}
}
ull solve(string& S, vector<oper>& ops) {
S = bestRoot(S);
ull N = S.size();
for (auto& [_, R] : ops) {
R %= N;
}
map<oper, ull> count = {{oper(), 1}};
ull ans = 0;
optional<oper> fixed = nonTrivialFixed(S);
oper op_count;
for (oper op : ops) {
op_count = op_count.comb(N, op);
ans += count[op_count];
if (fixed.has_value()) {
oper opf = op_count.inv(N, *fixed);
assert(opf.comb(N, *fixed) == op_count);
ans += count[opf];
}
count[op_count] += 1;
}
return ans;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string S;
cin >> S;
ull K;
cin >> K;
vector<oper> ops(K);
fore(i, 0, K) {
char c;
cin >> c;
if (c == 'I') {
ops[i].I = true;
} else {
ull D;
cin >> D;
if (c == 'R') {
ops[i].R = D;
} else {
ops[i].R = S.size() - D;
}
}
}
auto ans = solve(S, ops);
cout << ans << '\n';
return EXIT_SUCCESS;
}