UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#184791#2068. 最长公共前缀snow_trace10044991ms281984kbC++112.8kb2023-09-14 09:30:422023-09-14 12:05:41

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mod1 = 1000000009;
const int mod2 = 1000000021;
const int base = 37;
int n,m,q,nw;
char a[2005][2005];
pair<short,short> mp[2005][2005][3];
bool is_prime(int x){
	for(int i = 2;i*i<=x;i++){
		if(x%i == 0)return 0;
	}return 1;
}string s[8005];
int pre1[8005][2005],pre2[8005][2005],pw1[10005],pw2[10005];
signed main(){
	//freopen("T3.out","w",stdout);
	srand(time(0));
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >>n >> m >> q;
//	n = 2000,m = 2000,q = 1000000;
//	cout << n << " " <<m << " " << q << endl;;
//	for(int i = 1;i<=n;i++)for(int j = 1;j<=m;j++)a[i][j] = 'a'+rand()%2;
	for(int i = 1;i<=n;i++)for(int j = 1;j<=m;j++)cin >> a[i][j];
	//H
	pw2[0] = pw1[0] = 1;for(int i =1;i<=10000;i++)pw1[i] = pw1[i-1]*base%mod1,pw2[i] = pw2[i-1]*base%mod2;
	for(int i = 1;i<=n;i++){
		nw++;s[nw]+="#";
		for(int j = 1;j<=m;j++){
			mp[i][j][0] = {nw,s[nw].size()};
			s[nw]+=a[i][j];
		}//cout << s[nw] << endl;
	}//V
	for(int j = 1;j<=m;j++){
		nw++;s[nw]+="#";
		for(int i = 1;i<=n;i++){
			mp[i][j][1] = {nw,s[nw].size()};
			s[nw]+=a[i][j];
		}
	}//D
	for(int p = 1;p<=n;p++){
		nw++;s[nw]+="#";
		for(int i = p,j = 1;i<=n and j<=m;i++,j++){
			mp[i][j][2] = {nw,s[nw].size()};
			s[nw]+=a[i][j];
		}
	}for(int p = 2;p<=m;p++){
		nw++;s[nw]+="#";
		for(int i = 1,j = p;i<=n and j<=m;i++,j++){
			mp[i][j][2] = {nw,s[nw].size()};
			s[nw]+=a[i][j];
		}
	}for(int i = 1;i<=nw;i++){
		pre1[i][0] = pre2[i][0] = 0;
		for(int j = 1;j<s[i].size();j++){
			pre1[i][j] = (pre1[i][j-1]*base+(s[i][j]-'a'))%mod1;
			pre2[i][j] = (pre2[i][j-1]*base+(s[i][j]-'a'))%mod2;
		}
	}int x1,y1,x2,y2,p1,p2,pos1,pos2,l,r,mid,v11,v12,v21,v22;char d1,d2;
	while(q--){
		cin >> x1 >> y1 >> d1 >> x2 >> y2 >> d2;
		//x1 = rand()%n+1,y1 = rand()%m+1,d1 = rand()%3,x2 = rand()%n+1,y2 = rand()%m+1,d2 = rand()%3;
		if(d1 == 'H')d1 = 0;if(d2 == 'H')d2 = 0;
		if(d1 == 'V')d1 = 1;if(d2 == 'V')d2 = 1;
		if(d1 == 'D')d1 = 2;if(d2 == 'D')d2 = 2;
		p1 = mp[x1][y1][d1].first,p2 = mp[x2][y2][d2].first,pos1 = mp[x1][y1][d1].second,pos2 = mp[x2][y2][d2].second;
		l = 0,r = min(s[p1].size()-pos1,s[p2].size()-pos2);
		//cout << p1 << " " << pos1 << " " << p2 << " " << pos2 <<endl;
		//cout << s[p1] << endl << s[p2] << endl;
		while(l<r){
			int mid = l+r+1>>1;
			v11 = (pre1[p1][pos1-1+mid]-pw1[mid]*pre1[p1][pos1-1]%mod1+mod1)%mod1,v21 = (pre1[p2][pos2-1+mid]-pw1[mid]*pre1[p2][pos2-1]%mod1+mod1)%mod1;
			v12 = (pre2[p1][pos1-1+mid]-pw2[mid]*pre2[p1][pos1-1]%mod2+mod2)%mod2,v22 = (pre2[p2][pos2-1+mid]-pw2[mid]*pre2[p2][pos2-1]%mod2+mod2)%mod2;
			//cout << mid << " " << v11 << " " << v21  << " " << pre1[p1][pos1-1+mid]<< endl;
			if(v11 == v21 and v12 == v22)l = mid;
			else r = mid-1;
		}cout << l << '\n';
	}return 0;
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 11
Accepted

Test #1:

score: 11
Accepted
time: 238ms
memory: 190992kb

input:

1000 2000 10000
awanannwnnaaawwnwawanwnwaaaanwnaaananwawwwwnannannawwwawwaaaaannwnwnwnnaaawaawawannn...

output:

0
0
0
0
0
0
0
2
0
1
0
0
1
0
1
0
2
0
1
2
0
0
2
0
1
1
0
0
0
0
1
0
0
0
1
0
0
1
0
1
1
0
2
0
1
3
1
0
3
0
...

result:

ok 10000 tokens

Test #2:

score: 0
Accepted
time: 230ms
memory: 192940kb

input:

2000 1000 10000
zzqzqqqqqzqqqqzqqqqzqzqzzzqqzqqqzzqzzqqqqqqqqqqqzzqzqqqzzqqzzqzqzzzqqzqqzzzzzzqzzzqq...

output:

0
3
3
1
0
2
2
2
1
0
1
0
0
0
1
0
2
0
6
1
0
1
1
0
2
1
1
1
2
4
0
1
3
2
1
0
1
2
0
0
0
1
1
0
1
1
0
4
0
3
...

result:

ok 10000 tokens

Subtask #2:

score: 24
Accepted

Test #3:

score: 24
Accepted
time: 2462ms
memory: 281980kb

input:

2000 2000 1000000
gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg...

output:

1146
541
203
322
618
166
345
138
520
206
1031
667
741
921
361
1110
1057
372
899
209
491
69
93
639
14...

result:

ok 1000000 tokens

Test #4:

score: 0
Accepted
time: 1638ms
memory: 281984kb

input:

2000 2000 1000000
svsvsvvsvssssvsvvvvssvvvssvsvsvssssvvsvvvsvvsssvssssvssvvsvvvssssssvsvvvvsssvvvsss...

output:

2
0
2
1
2
0
0
1
3
1
0
0
0
1
0
1
0
1
0
6
1
2
2
1
3
5
0
0
1
1
0
0
2
1
1
3
1
0
0
2
1
0
0
0
0
0
1
0
5
0
...

result:

ok 1000000 tokens

Test #5:

score: 0
Accepted
time: 2018ms
memory: 281980kb

input:

2000 2000 1000000
qqqqqqqqqqqqqqqqcqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...

output:

327
50
54
421
87
4
189
135
200
214
55
578
82
72
335
52
45
110
521
267
123
21
293
105
105
479
85
69
1...

result:

ok 1000000 tokens

Test #6:

score: 0
Accepted
time: 2226ms
memory: 281984kb

input:

2000 2000 1000000
ssssssssssssssssssssssssssgsssssssssssssssssssssssssssssssssssssssssssssssssssssss...

output:

187
336
26
367
487
65
159
30
12
275
628
200
252
742
362
7
316
232
26
82
607
158
56
444
155
262
1
106...

result:

ok 1000000 tokens

Test #7:

score: 0
Accepted
time: 2545ms
memory: 281980kb

input:

2000 2000 1000000
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh...

output:

1281
105
890
1087
24
179
172
588
260
206
238
255
150
346
484
224
369
293
15
275
299
125
19
406
1146
...

result:

ok 1000000 tokens

Test #8:

score: 0
Accepted
time: 1607ms
memory: 281980kb

input:

2000 2000 1000000
cschgfyhvrlhmuchmcssqmxypkfkmkrfwpivfljvlascqlphvlbmjnztypqeoyevqecixdhricknhdrero...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 1000000 tokens

Test #9:

score: 0
Accepted
time: 2062ms
memory: 281980kb

input:

2000 2000 1000000
cwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcwcw...

output:

0
0
0
0
118
0
0
0
0
0
0
0
0
0
0
0
0
0
178
0
0
0
297
0
0
0
0
0
0
63
0
0
0
115
0
0
0
699
955
0
325
0
0...

result:

ok 1000000 tokens

Subtask #3:

score: 22
Accepted

Test #10:

score: 22
Accepted
time: 2662ms
memory: 281980kb

input:

2000 2000 1000000
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii...

output:

1182
916
1440
420
469
1353
818
143
458
743
25
1414
878
23
284
453
324
1652
10
556
276
448
1143
916
1...

result:

ok 1000000 tokens

Test #11:

score: 0
Accepted
time: 1665ms
memory: 281984kb

input:

2000 2000 1000000
ddddoddoodoodooddodododdodododdoooddodddddoodoodddddoodddodooddoddooooodddooodddoo...

output:

4
1
1
1
2
0
0
0
4
2
1
1
3
0
0
0
1
4
1
1
0
0
0
2
1
2
1
0
2
0
2
0
2
0
4
4
1
1
0
0
2
0
5
2
0
1
2
1
0
0
...

result:

ok 1000000 tokens

Test #12:

score: 0
Accepted
time: 2038ms
memory: 281980kb

input:

2000 2000 1000000
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd...

output:

180
74
19
380
126
169
75
17
53
352
123
150
258
103
65
17
87
109
11
164
407
143
241
407
214
256
84
22...

result:

ok 1000000 tokens

Test #13:

score: 0
Accepted
time: 2385ms
memory: 281980kb

input:

2000 2000 1000000
gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg...

output:

36
41
642
9
356
1100
529
595
2
299
253
559
761
245
494
259
43
234
430
885
449
225
404
211
213
32
38
...

result:

ok 1000000 tokens

Test #14:

score: 0
Accepted
time: 2553ms
memory: 281980kb

input:

2000 2000 1000000
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd...

output:

905
292
231
537
281
229
247
240
1460
110
261
530
378
569
206
163
1185
128
983
117
296
79
123
3
53
81...

result:

ok 1000000 tokens

Test #15:

score: 0
Accepted
time: 1635ms
memory: 281984kb

input:

2000 2000 1000000
nvmqmtwqkbpisvkrhgehqvohvizhldbhyiamavjuipxwxeqcqiqwcrjjlxeuvgzpgfwtcdgpymkptfemtv...

output:

0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 1000000 tokens

Test #16:

score: 0
Accepted
time: 1595ms
memory: 281984kb

input:

2000 2000 1000000
tctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctctc...

output:

0
0
0
0
0
0
0
0
0
0
1
0
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
344
0
0
0
0
0
0
0
0
0
0
0
1
0
0
546
0
0
0
1
1
...

result:

ok 1000000 tokens

Subtask #4:

score: 43
Accepted

Test #17:

score: 43
Accepted
time: 1346ms
memory: 190996kb

input:

1000 2000 1000000
yzyzzyaazyyzzaaayzzayyyaaayyazayyzzyazzyazayzayzyzzayyzyzzayzazzyzzayzaazazaaayzyy...

output:

2
0
2
1
1
0
1
0
0
1
0
1
0
0
0
1
1
0
0
0
0
1
0
0
0
1
1
0
0
0
1
0
2
0
1
0
0
0
0
2
1
2
0
1
0
0
0
2
0
0
...

result:

ok 1000000 tokens

Test #18:

score: 0
Accepted
time: 2098ms
memory: 281980kb

input:

2000 2000 1000000
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...

output:

255
79
826
62
931
351
68
1020
265
697
2
1101
779
606
176
1568
696
615
106
596
185
311
65
397
816
25
...

result:

ok 1000000 tokens

Test #19:

score: 0
Accepted
time: 1658ms
memory: 281980kb

input:

2000 2000 1000000
yyyvvyyyvyyyyvvvyyvyyyyvyyyyyyvvyvvyyyyyvvyyvvyvyvyvvvyyyvyvyvyyvyyyyvyyyvyyyvvyvy...

output:

3
0
0
0
0
1
0
0
0
0
1
2
0
2
1
0
0
0
4
2
0
0
3
1
1
2
3
1
0
1
1
0
0
3
0
0
0
2
0
0
1
0
1
0
0
3
2
0
0
1
...

result:

ok 1000000 tokens

Test #20:

score: 0
Accepted
time: 2086ms
memory: 281980kb

input:

2000 2000 1000000
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj...

output:

128
57
641
99
820
2
155
264
232
511
552
36
506
522
31
528
574
506
638
59
127
22
103
53
30
56
14
18
1...

result:

ok 1000000 tokens

Test #21:

score: 0
Accepted
time: 2096ms
memory: 281984kb

input:

2000 2000 1000000
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...

output:

1
33
165
12
417
104
452
86
315
401
295
141
60
41
76
115
118
150
489
69
46
196
373
109
142
34
115
481...

result:

ok 1000000 tokens

Test #22:

score: 0
Accepted
time: 2529ms
memory: 281984kb

input:

2000 2000 1000000
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii...

output:

371
247
218
9
492
569
21
454
121
168
207
251
176
236
333
23
138
1501
385
306
342
101
286
204
168
99
...

result:

ok 1000000 tokens

Test #23:

score: 0
Accepted
time: 1611ms
memory: 281980kb

input:

2000 2000 1000000
ccbxyakkmwxjxctespteblasatcayufpdpqvjjbmatywntdpcnhmwabgpmsyuxvqjtgxutglopldstpcmq...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
...

result:

ok 1000000 tokens

Test #24:

score: 0
Accepted
time: 2008ms
memory: 281984kb

input:

2000 2000 1000000
mfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmfmf...

output:

0
0
0
0
2
0
0
1
0
1
0
0
0
0
0
0
2
1
0
361
0
0
0
1
0
0
0
0
1
1
0
1
0
0
1
0
0
0
0
0
323
0
0
0
1
0
0
1
...

result:

ok 1000000 tokens