QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#558523#8475. Palindrome StringsHarry27182AC ✓177ms276980kbC++142.7kb2024-09-11 16:35:582024-09-11 16:35:59

Judging History

你现在查看的是最新测评结果

  • [2024-09-11 16:35:59]
  • 评测
  • 测评结果:AC
  • 用时:177ms
  • 内存:276980kb
  • [2024-09-11 16:35:58]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
char S[2000005];string s,t[100005];
int len[2000005],num[1000005],tr[1000005][26],n,m,nxt[1000005],in[1000005];
int val[1000005],val2[1000005],tot,h[4][1000005],p[2][1000005];
const int mod1=1000000007,mod2=1000000009;
void Manacher()
{
	S[0]='@';
	for(int i=1;i<=n;i++)S[2*i-1]='#',S[2*i]=s[i-1];
	S[2*n+1]='#';S[2*n+2]='%';
	int l=0,r=0;
	for(int i=1;i<=2*n+1;i++)
	{
		if(l&&r)len[i]=min(len[l+r-i],r-i);
		while(S[i-len[i]-1]==S[i+len[i]+1])len[i]++;
		if(i+len[i]>r){l=i-len[i];r=i+len[i];}
		num[(i-len[i]+1)/2]++;num[i/2+1]--;
	}
	for(int i=1;i<=n;i++)num[i]+=num[i-1];
}
void ins(string s)
{
	int u=0,len=s.length();
	for(int i=0;i<len;i++)
	{
		int c=s[i]-'a';
		if(!tr[u][c])tr[u][c]=++tot;
		u=tr[u][c];
	}
}
void build()
{
	queue<int>q;
	for(int i=0;i<26;i++)if(tr[0][i])q.push(tr[0][i]);
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(int i=0;i<26;i++)
		{
			if(tr[u][i])nxt[tr[u][i]]=tr[nxt[u]][i],in[nxt[tr[u][i]]]++,q.push(tr[u][i]);
			else tr[u][i]=tr[nxt[u]][i];
		}
	}
}
void search(string s)
{
	int u=0;
	for(int i=0;i<n;i++)
	{
		int c=s[i]-'a';
		u=tr[u][c];
		val[u]++;
		if(i+2<=n)val2[u]+=num[i+2];
	}
}
void topo()
{
	queue<int>q;
	for(int i=0;i<=tot;i++)if(!in[i])q.push(i);
	while(!q.empty())
	{
		int u=q.front();q.pop();
		in[nxt[u]]--;val[nxt[u]]+=val[u];val2[nxt[u]]+=val2[u];
		if(!in[nxt[u]])q.push(nxt[u]);
	}
}
int query(int x,int l,int r)
{
	int mod=(x%2==0?mod1:mod2);
	if(x<=1)return (h[x][r]-(l==0?0:1ll*h[x][l-1]*p[x][r-l+1]%mod)+mod)%mod;
	else return (h[x][l]-1ll*h[x][r+1]*p[x-2][r-l+1]%mod+mod)%mod;
}
signed main()
{
	cin.tie(0)->sync_with_stdio(0);
	cin>>n>>m>>s;reverse(s.begin(),s.end());
	Manacher();
	for(int i=1;i<=m;i++)
	{
		cin>>t[i];
		ins(t[i]); 
	}
	build();search(s);topo();
	for(int i=1;i<=m;i++)
	{
		int u=0,len=t[i].length();
		for(int j=0;j<len;j++)
		{
			int c=t[i][j]-'a';
			u=tr[u][c];
		}
		int res=val2[u];p[0][0]=p[1][0]=1;h[2][len]=h[3][len]=0;
		for(int j=0;j<len;j++)
		{
			h[0][j]=(j?(1ll*h[0][j-1]*131%mod1+t[i][j])%mod1:t[i][j]);
			h[1][j]=(j?(1ll*h[1][j-1]*131%mod2+t[i][j])%mod2:t[i][j]);
			p[0][j+1]=1ll*p[0][j]*131%mod1;
			p[1][j+1]=1ll*p[1][j]*131%mod2;
		}
		for(int j=len-1;j>=0;j--)
		{
			h[2][j]=(j<len-1?(1ll*h[2][j+1]*131%mod1+t[i][j])%mod1:t[i][j]);
			h[3][j]=(j<len-1?(1ll*h[3][j+1]*131%mod2+t[i][j])%mod2:t[i][j]);
		}
		u=0;
		for(int j=0;j<len;j++)
		{
			int c=t[i][j]-'a';
			u=tr[u][c];
			if(query(0,j+1,(j+len)/2)==query(2,(j+len+1)/2,len-1)&&query(1,j+1,(j+len)/2)==query(3,(j+len+1)/2,len-1))res+=val[u];
		}
		cout<<res<<'\n';
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 26340kb

input:

8 3
icpccamp
p
c
pc

output:

4
7
4

result:

ok 3 number(s): "4 7 4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 28180kb

input:

10 3
bbbabbbbbb
baaaa
abb
bb

output:

10
4
31

result:

ok 3 number(s): "10 4 31"

Test #3:

score: 0
Accepted
time: 0ms
memory: 26168kb

input:

10 4
baababaaba
aaaaa
a
ab
aa

output:

8
18
17
11

result:

ok 4 number(s): "8 18 17 11"

Test #4:

score: 0
Accepted
time: 4ms
memory: 9260kb

input:

10000 1000
aabababbbaaabbbabbbbbbbbbaaabaabbbabbaababbbaaabbaabbbbbabababbbbbbbabbabaabaababbabaaababbbbaaabaababaaaaabbbbbbabbbababaababbbbabbbbabbabbbbbbabbaababbbbbbabaabababbbaabaaabbbbaaaabaaababbaaabbbbabababbaaabababababaababaabbaaabbaabababbbaabbbabaabbbbbbbbaabbaaabbbaabbaabbbabaabababbbbbb...

output:

300
18
197
198
181
21
6
5
21440
9
283
23680
8422
56163
35120
56163
35120
56163
35120
35120
56163
56163
56163
56163
56163
56163
56163
35120
35120
56163
56163
35120
56163
35120
35120
35120
35120
35120
56163
35120
35120
35120
56163
56163
35120
56163
35120
56163
35120
35120
56163
56163
56163
56163
35120...

result:

ok 1000 numbers

Test #5:

score: 0
Accepted
time: 0ms
memory: 12000kb

input:

20000 2000
bbbaaaaaababbbaababbaaaababaaabbaabbbbaaabbbaaababaaaaabbbbbbbaaaaabaaabbabababbbbbbbbaabaabbbbabaaabbbababaaabbabaabbbabbbbaaaababaabbbabbaaaababababbbbaabaababbbbbbbbabbaaaaaaaaaabbbbbaaaaaabbababbbaabbaabaaaabbabababaaabaabbabaabaaaabbbbbababaababbbbbababaaabaabbbbaaabaaababaabaaabbbab...

output:

1212
6
834
6
114
62
62
2309
89183
0
100016
90112
169223
169657
169657
169657
169223
169657
169223
169223
169657
169223
169223
169657
169657
169657
169657
169223
169223
169657
169657
169657
169223
169657
169657
169223
169657
169223
169657
169657
169657
169657
169223
169223
169657
169223
169657
169657...

result:

ok 2000 numbers

Test #6:

score: 0
Accepted
time: 5ms
memory: 9388kb

input:

30000 3000
aaaabbbaaaabaaaabaabaaaaaabaabbabaaaaaaaabbaabbaaaabbbbbababbbbbaabbabbabbbaaaababaaaabbbababaaabaabbbaabaababaabbababaaabbaaababbbaaaaaababaabbbaaabbabbbbaababbababaaaaaaaabbbaaaaabbabbbabbabbbaabbaabaaaaabbbbaabbbabbaabaabbaaaaaaaabbabbbbabaababbbabaaaabbbbbbbaabbbaabaaaaababbbbaabaabaa...

output:

90
10172
41
272796
99
20374
47982
64339
3142
198
859
167521
48
93
30100
399
93
20190
64339
3146
185181
2841
48
211
30004
859
11
46357
768
64339
210045
5076
3792
162
63
203
9
34220
137
3
399
2709
289073
4308
26270
289073
209
64339
64339
10520
38330
2755
10925
97
2748
26138
3320
3228
3154
3825
43
1725...

result:

ok 3000 numbers

Test #7:

score: 0
Accepted
time: 8ms
memory: 21772kb

input:

60000 6000
abaaabbbaaabbbbaabbbbbaaabaaaaabaaabaaaababaabbbbabbbaaabababbbababaababbbbaaabbaaaaaaabaaaabaaaaaaababaababaabbaaababaababaaabbbabbabaaaabbaabbbababbbbaabababbbbaabababaaababbbababaabbaaaaaabbbbbbbbabaaaabbbbaabaabaaabaababbaabbaabaabbbbbabbabbbbabbabbbbaaabbbbaaabbbababaabbabbbaaaabbbaa...

output:

306
296
289
289
247
6
459
373
14834
2363
443
14565
14220
4181
2130
0
0
1081983
1052603
84292
111982
111982
1081983
1081983
111982
1081983
1081983
1081983
111982
1081983
1081983
1081983
1081983
1081983
1081983
1081983
1081983
111982
111982
1081983
111982
1081983
1081983
111982
1081983
1081983
1081983...

result:

ok 6000 numbers

Test #8:

score: 0
Accepted
time: 8ms
memory: 46720kb

input:

100000 10000
babbabbbbabbbbbbaabaaabbabbabbaaabaaaaaabaabbbbbbababbaaaaabbabbbababaaaaaabbababbaababbabbbbabaabbaabbbabbbbbbabbaaabbaaaaabbabaababbabaaabaabbbabbaaabaaabababbbbbaabaabbbbabbaabbbbbaabaaabbbbbbbbabbbbbbaabaabbbbaaaabaabbbbbbbbbbaabbabaabbaabbaaaaaabbbaaabbabbbaaabababbaabaabaaaababaaa...

output:

5
32
37
318
8
481
226276
478914
50487
717062
490267
490267
490267
490267
490267
490267
490267
717062
717062
717062
490267
717062
717062
717062
717062
717062
490267
717062
490267
717062
490267
490267
490267
490267
490267
717062
490267
717062
717062
717062
490267
717062
717062
717062
717062
490267
490...

result:

ok 10000 numbers

Test #9:

score: 0
Accepted
time: 13ms
memory: 37436kb

input:

200000 20000
bbaababaabaababbaaaaabbabaabbbbbbaaaaabbbbaabbaabbbaaaaaaabbabbbababbbaabababbbbabbabbbabbaabaabbaaaabaaabaabbaaaaabbaaaabaaababababbabbaababbbababaababbaabaabaaaabaaabbabaaaabababbabaaaabbabbbbaaabbabaaabbbbbbabababbababbaaabbbabbaaaaaaaaababaaabbaaaaabaabbaaaaaaabbaaabaabbbbbbbbbbbabb...

output:

238781
1211940
8405
849154
259585
17350
177472
50750
93220
55963
328912
242379
449236
1915
34985
91319
18087
45363
849154
640
123841
9778
3158
82043
849154
120751
128
328912
53022
53022
45108
849154
744
60541
319083
379704
52988
877813
118341
379704
228832
228832
39886
2377
386954
126842
5273
8444
1...

result:

ok 20000 numbers

Test #10:

score: 0
Accepted
time: 67ms
memory: 146116kb

input:

500000 50000
aabbbbaabbaabaaabbbbaaaaaaabaabaaaabbbbbabbbbbabaabbbabaaabaaaababaaaababbabbbbbaabbababbbbbbaaaabaaaaabbabaaabababbbbbbababbbabaaabbaaaabaababbbaaaabbabaabaaabaabbbaabbabbabbaaaabbbbababbabbbaaaaabaaaaaaabaaaabaaabbaabaaaabaabaabbbaabbaaabbbabbabbaaababbabbbbbbbaaaabaabbbabababaabaabab...

output:

253
42
200
3720
20
5
13205
3578
3506
84
49
4592
35
5614
35
84528
85656
89730
75979
9884
806454
8394154
1720728
1720728
1720728
1720728
8394154
8394154
1720728
1720728
1720728
1720728
1720728
8394154
8394154
1720728
1720728
8394154
8394154
1720728
1720728
8394154
8394154
8394154
1720728
8394154
83941...

result:

ok 50000 numbers

Test #11:

score: 0
Accepted
time: 49ms
memory: 52612kb

input:

1000000 50000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
5...

result:

ok 50000 numbers

Test #12:

score: 0
Accepted
time: 136ms
memory: 114244kb

input:

1000000 50000
aabbabababbabaabababaabaabbbbbabaabaabbbbbbaababaababaabbabbaabaaabaabaaabbaaababaabbbaaaaababbaaaaaabaaaaabbaabbbaaababaabbaabbaaaaaababababbbaababaaabbaaaabbbaabbbbbaaaabababaaabbabbabaaababaabbaaabbaabaababaabaaabbabbabababaababbbbabbaabbbabbababbbbbbbaaaaabaababababbaababbbbaabbaab...

output:

453582
3691
29106
114479
1795
131831239
275852
16751
58921
41
14559
64836
7714
56334
339711
131996707
3434
597893
2021
13999
3394
28
29474
2297
223
15958
1012337
56
57070
308
656760
58595
1934
1040797
0
911007
312293
939
3782
0
6224
132522558
1469
58985
16094
68026
76048
341200
70
680572
7141
132522...

result:

ok 50000 numbers

Test #13:

score: 0
Accepted
time: 137ms
memory: 130280kb

input:

1000000 50000
gpkkgebfroojkwdrldwydyslbndoqzfnfpsntbmucbptxqpmtabzhnvbkrpdlbntjqxqaqanrgcrvfjtjffemhwxjsqaysfrwkksutrgfrzhyvrvzhadcerhulyqticyyolrcavmodmbwfiiztrdneqteahjawvvtcrhyfethwkfrdmcjfudmnnydmvtjsiwlbtjjscluokjmejrjtsvfftlkmgurrincjptopmiqxmfdmwvplbczazwniykbayyawwilcetzmornbjwputymqfkxjzjfj...

output:

3
35
627583
102598
4
3
773730
246180
410408
197004
201623
229772
3
425353
197004
427227
3
86214
29
77295
415011
4
3
3
3
4
143558
14230
62775
604
229772
93706
65530
3
3
32
228349
4
180620
754960
77295
235128
77295
410392
3
93705
41878
229772
278357
27959
135383
3
3
3
81914
3
377624
4
197004
197005
77...

result:

ok 50000 numbers

Test #14:

score: 0
Accepted
time: 104ms
memory: 121800kb

input:

1000000 80000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
5...

result:

ok 80000 numbers

Test #15:

score: 0
Accepted
time: 177ms
memory: 269320kb

input:

1000000 80000
bbbaaababbabbbbaabbaaababaabaababbaaaaabbabaaabbbbbaaaaababbaabbabbbbaaaaaaababbbbabaabbaababababbabaabbaabbabbaaabaaaabaaaabaabaaaabbbaaaababababababbabbaababbbaaabbababbaaaaabaaabbaaabbabaababababaabaabaababbbbababbabbaaabbbbbabababbabbbbaababbabbaaaaaaaaababaaabaaaababaabbaaaaababba...

output:

5
6187
1631
3770
306
54
2805
2240
10196
2400
6002
1498910
1517405
36
12228
1622220
1535229
1632153
12314
12865460
867575
57710
4042522
301786
12865460
15927056
138406043
15927056
138406043
15927056
15927056
138406043
15927056
138406043
15927056
138406043
15927056
138406043
138406043
15927056
1592705...

result:

ok 80000 numbers

Test #16:

score: 0
Accepted
time: 174ms
memory: 276980kb

input:

1000000 80000
oidqopacrekstvsdtehxmnftwdzkiadmtozoqosxslggaxllsvxtvvifeolzpxkmvczffvqhesfsfvhrjnwfgssgbxrhrosfyihzueyyajlkzbetsmalnjzvfcniatydwwxyvtstohtllqglhoblranwxygzgocghqzitbvmtwjoroztohhcdqkqjlmkpemobklvwtampfufwjfxxxmvzjheckiaxqqpewztkpntgemnvrigycqwrhgifbwntolgwgzexsdvjasxgwsplofvvccdqvhblh...

output:

3
1076
1021
1238
1275
3
8960
1168
130414
1072
4800
48601
204362
10457
1025979
1025979
337
1808318
14435961
14435961
14435961
414518
14435961
414518
14435961
3647240
24538
14435961
414518
3647240
14435961
31741
1808318
14435961
14435961
3647240
14435961
14435961
14435961
14435961
14435961
14435961
14...

result:

ok 80000 numbers

Test #17:

score: 0
Accepted
time: 53ms
memory: 38368kb

input:

1000000 100000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
5...

result:

ok 100000 numbers

Test #18:

score: 0
Accepted
time: 71ms
memory: 49644kb

input:

1000000 100000
baababbbbbabaabaaaaaaaaaaababbaaabbabbaabbbbbbabbaabaaaabaababababbbaabaabaabbaabaabbbbaaaaaabababaababbabbbbabbbaaabbaabbbabbaaabbbabaabbaabababbaaabbbababababbaaababbbbaaaabaabababbabababbaabaabbbabababbabaabbbaaabaabbbaaabbbabbaaabaaabaaabaaabaabbabababbabbbaaababbababaababaababaaa...

output:

528492
4081
1000552
3830
4844288
754126
3066616
130136
317839
1120765
19773
418846
1000561
50560
1076075
190499
2316299
57518
183185
18314
1920
14804
284548
496
683767
424224
1597916
468144
1066244
1052028
1000000
14573
561427
884989
3648
48198
52708
424224
5635904
505383
1120765
106290
596899
14771...

result:

ok 100000 numbers

Test #19:

score: 0
Accepted
time: 124ms
memory: 92816kb

input:

1000000 100000
zmxzthulqsoskialeouxnnwclmqkpjdzydjphfwyxazjanaigofbikqdwuepmsjdinnafffdwknmiohwisluowkkdjgywobvhegdpxdizryoovlbasghqcwpzmnhhrnxzkrcckpobarlegivwraueghjsrektnhtwlkewutlbxqxkxgswefylnruevdcaknzijvclriyxnoqukjourmvsobpcynccqthvhicacgtctzkgzhxmepjrjtossxctxjpljsvbzexvfnjliuntxnqxvuuhrzwz...

output:

84369
272920
496688
3
22
562224
364659
4
3
48931
140556
100998
496688
256536
399585
3
4
21
496688
132364
589721
54466
3
563781
346909
68230
3
3
72326
3
115980
3
305688
12908
137862
463920
51019
3
3
3
256536
500838
24
272920
3
3
322092
562224
3
463920
240152
3
321036
961084
155270
496688
594992
14548...

result:

ok 100000 numbers