UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#213366#2068. 最长公共前缀nodgd246661ms52128kbC++1.7kb2024-11-11 20:23:002024-11-11 23:03:47

answer

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2000 + 5;
const int B = 31, P = 999999001;

int N, M, Q;
char a[MAX_N][MAX_N];
int pw[MAX_N], hv[MAX_N][MAX_N], hh[MAX_N][MAX_N], hd[MAX_N][MAX_N];

int main() {
	scanf("%d%d%d", &N, &M, &Q);
	for (int i = 1; i <= N; i ++) {
		scanf("%s", a[i] + 1);
	}
	pw[0] = P - 1;
	for (int i = 1; i <= N || i <= M; i ++) {
		pw[i] = 1ll * pw[i - 1] * B % P;
	}
	for (int i = N; i >= 1; i --) {
		for (int j = M; j >= 1; j --) {
			hh[i][j] = (1ll * hh[i][j + 1] * B + (a[i][j] - 'a' + 1)) % P;
			hv[i][j] = (1ll * hv[i + 1][j] * B + (a[i][j] - 'a' + 1)) % P;
			hd[i][j] = (1ll * hd[i + 1][j + 1] * B + (a[i][j] - 'a' + 1)) % P;
		}
	}
	for (int i = 1; i <= Q; i ++) {
		int x1, y1, x2, y2;
		char d1[5], d2[5];
		scanf("%d%d%s%d%d%s", &x1, &y1, &d1, &x2, &y2, &d2);
		int len1 = d1[0] == 'V' ? N - x1 + 1 : d1[0] == 'H' ? M - y1 + 1 : min(N - x1 + 1, M - y1 + 1);
		int len2 = d2[0] == 'V' ? N - x2 + 1 : d2[0] == 'H' ? M - y2 + 1 : min(N - x2 + 1, M - y2 + 1);
		int l = 0, r = max(len1, len2);
		while (l < r) {
			int m = l + r + 1 >> 1;
			int t1 = 0, t2 = 0;
			if (d1[0] == 'V') {
				t1 = (hv[x1][y1] + 1ll * hv[x1 + m][y1] * pw[m]) % P;
			} else if (d1[0] == 'H') {
				t1 = (hh[x1][y1] + 1ll * hh[x1][y1 + m] * pw[m]) % P;
			} else {
				t1 = (hd[x1][y1] + 1ll * hd[x1 + m][y1 + m] * pw[m]) % P;
			}
			if (d2[0] == 'V') {
				t2 = (hv[x2][y2] + 1ll * hv[x2 + m][y2] * pw[m]) % P;
			} else if (d2[0] == 'H') {
				t2 = (hh[x2][y2] + 1ll * hh[x2][y2 + m] * pw[m]) % P;
			} else {
				t2 = (hd[x2][y2] + 1ll * hd[x2 + m][y2 + m] * pw[m]) % P;
			}
			if (t1 == t2) {
				l = m;
			} else {
				r = m - 1;
			}
		}
		printf("%d\n", l);
	}
	return 0;

}

Details

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

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 11
Accepted
time: 27ms
memory: 26672kb

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: -11
Runtime Error

input:

2000 1000 10000
zzqzqqqqqzqqqqzqqqqzqzqzzzqqzqqqzzqzzqqqqqqqqqqqzzqzqqqzzqqzzqzqzzzqqzqqzzzzzzqzzzqq...

output:


result:


Subtask #2:

score: 24
Accepted

Test #3:

score: 24
Accepted
time: 855ms
memory: 52128kb

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: 855ms
memory: 52128kb

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: 1364ms
memory: 52124kb

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: 1119ms
memory: 52128kb

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: 885ms
memory: 52124kb

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: 735ms
memory: 52124kb

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: 821ms
memory: 52124kb

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: 0
Runtime Error

Test #10:

score: 0
Runtime Error

input:

2000 2000 1000000
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii...

output:


result:


Subtask #4:

score: 0
Skipped