-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCC - CHSTR.cpp
316 lines (272 loc) · 5.62 KB
/
CC - CHSTR.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* https://www.codechef.com/problems/CHSTR */
// Solution using Z algorithm
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define fr(i,j,k) for(i = j; i < (k); i++)
#define all(x) x.begin(), x.end()
#define el '\n'
#define remax(a,b) a = max(a, b)
#define remin(a,b) a = min(a, b)
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<pii> vpi;
// --------------------------- TEMPLATE ENDS -------------------------------------
const pii dxy[] = { {-1, 0}, {1, 0}, {0, 1}, {0, -1} };
const int mod = 1e9+7;
const int inf = 2e18;
const ld eps = 1e-9;
const int NN = 1e5 + 2;
// ------- O(N) space, O(1) time per query --------
const int maxn = NN;
int fact[maxn], inv[maxn], invFact[maxn];
int ncr(int n, int r){
if(n < 0 || r < 0 || n < r) return 0;
int v = (invFact[r] * invFact[n-r]) %mod;
return (v*fact[n])%mod;
}
void pre(){
int i;
fact[0] = fact[1] = 1;
for(i = 2; i < maxn; i++) {
fact[i] = (fact[i-1]*i)%mod;
}
inv[0] = 0;
inv[1] = 1;
for (i = 2; i < maxn; i++) {
inv[i] = mod - ((int) (mod / i) * inv[mod % i] % mod);
}
invFact[0] = 1;
for (i = 1; i < maxn; i++) {
invFact[i] = (inv[i] * invFact[i - 1] % mod);
}
}
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true; string res = "{";
for (const auto &x : v) {
if (!first) { res += ", "; }
first = false; res += to_string(x);
}
res += "}"; return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
// #define TRACE
#ifdef TRACE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
vi z_function(int shift, string &s) {
int n = s.length();
vi res = {n-shift};
int L = 0, R = 0;
for (int i = shift+1; i < n; i++) {
if (i > R) {
L = R = i;
while (R < n && s[R-L+shift] == s[R]) R++;
res.eb(R-L);
R--;
} else {
int k = i-L;
if (res[k] < R-i+1) res.pb(res[k]);
else {
L = i;
while (R < n && s[R-L+shift] == s[R]) R++;
res.pb(R-L);
R--;
}
}
}
return res;
}
string s;
void solve(){
int i = 0, j = 0, k = 0, n = 0, m = 0;
cin >> n >> m;
cin >> s;
vi cum(n+2, 0), cnt(n+2);
fr(i, 0, n) {
vi z = z_function(i, s);
debug(i, z);
sort(all(z));
for(k = 1, j = 0; k <= n-i; k++) {
while(j < z.size() and z[j] < k){
j++;
}
if(j < z.size())
cum[z.size() - j]++;
}
debug(cum);
}
fr(i, 1, n+1) {
cnt[i] = cum[i] - cum[i+1];
}
// debug(cum, cnt);
vector<int> ans(n+1, 0);
fr(i, 1, n+1) {
fr(j, i, n+1) {
ans[i] += (ncr(j, i)*cnt[j] % mod);
ans[i] %= mod;
}
}
while(m--) {
cin>>k;
if(k > n) cout<<"0\n";
else {
cout << ans[k]<<el;
}
}
}
int32_t main(){
#ifndef DEBUG
ios::sync_with_stdio(false); cin.tie(0);
#endif
pre();
int T = 1, tc;
cin >> T;
for(tc = 1; tc <= T; tc++){
solve();
}
return 0;
}
// Solution using Trie
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define fr(i,j,k) for(i = j; i < (k); i++)
#define all(x) x.begin(), x.end()
#define el '\n'
#define remax(a,b) a = max(a, b)
#define remin(a,b) a = min(a, b)
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<pii> vpi;
// --------------------------- TEMPLATE ENDS -------------------------------------
const pii dxy[] = { {-1, 0}, {1, 0}, {0, 1}, {0, -1} };
const int mod = 1e9+7;
// const int inf = 2e18;
const ld eps = 1e-9;
const int NN = 5001;
long long fact[NN], inv[NN], invFact[NN];
long long ncr(int n, int r){
if(n < 0 || r < 0 || n < r) return 0;
int v = (invFact[r] * invFact[n-r]) %mod;
return (v*fact[n])%mod;
}
void pre(){
int i;
fact[0] = fact[1] = 1;
for(i = 2; i < NN; i++) {
fact[i] = (fact[i-1]*i)%mod;
}
inv[0] = 0;
inv[1] = 1;
for (i = 2; i < NN; i++) {
inv[i] = mod - ((int) (mod / i) * inv[mod % i] % mod);
}
invFact[0] = 1;
for (i = 1; i < NN; i++) {
invFact[i] = (inv[i] * invFact[i - 1] % mod);
}
}
const int MAXLEN = 2500, ALPHA = 26;
int trie[NN * MAXLEN][ALPHA], so = 0;
int trie_cnt[NN * MAXLEN];
string s;
vi cum, cnt;
void insert(int ind) {
int n = s.size();
for(int i = ind, u = 1; i < n; i++) {
int to = s[i] - 'a';
if(trie[u][to]) {
u = trie[u][to];
trie_cnt[u]++;
} else {
so++;
trie[u][to] = so;
u = so;
trie_cnt[u] = 1;
}
cum[trie_cnt[u]]++;
}
}
void solve(){
int i = 0, j = 0, k = 0, n = 0, m = 0;
cin>>n>>m;
cin>>s;
fr(i, 0, n*MAXLEN) {
fr(j,0,26) {
trie[i][j] = 0;
}
trie_cnt[i] = 0;
}
cum.assign(n+2, 0);
cnt.assign(n+2, 0);
so = 1;
for(i = n-1; i >= 0; i--) {
insert(i);
debug(cum);
}
fr(i, 1, n+1) {
cnt[i] = cum[i] - cum[i+1];
}
vector<long long> ans(n+1, 0);
fr(i, 1, n+1) {
fr(j, i, n+1) {
ans[i] += (ncr(j, i)*cnt[j]*1LL % mod);
ans[i] %= mod;
}
}
debug(cnt, ans);
while(m--) {
cin>>k;
if(k > n) cout<<"0\n";
else {
cout << ans[k]<<el;
}
}
}
int32_t main(){
#ifndef TRACE
ios::sync_with_stdio(false); cin.tie(0);
#endif
pre();
int T = 1, tc;
cin >> T;
for(tc = 1; tc <= T; tc++){
solve();
}
return 0;
}