UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213427#2068. 最长公共前缀one_zero_four_zero11184ms10420kbC++111.5kb2024-11-11 22:02:402024-11-11 23:10:06

answer

#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

int N, M, Q;
int cur = 2010;
string S[2005];
string R[2005], C[2005], D[4105];
int a, b, x, y;
char d1, d2;
int dir[131][2];

void __init(){
	return;
}

bool check(int l, int a, int b, int x, int y){
	if (a + dir[d1][0] * (l - 1) > N) return 0;
	if (b + dir[d1][1] * (l - 1) > M) return 0;
	if (x + dir[d2][0] * (l - 1) > N) return 0;
	if (y + dir[d2][1] * (l - 1) > M) return 0;
	while (l --){
		if (S[a][b] != S[x][y]) return 0;
		a += dir[d1][0], b += dir[d1][1];
		x += dir[d2][0], y += dir[d2][1];
	}
	return 1;
}

int solve(){
	int res = 0, l = 0, r = 2005;
	while (l <= r){
		int mid = (l + r) >> 1;
		if (check(mid, a, b, x, y)){
			res = mid;
			l = mid + 1;
		}else{
			r = mid - 1;
		}
	}
	return res;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("../data.in", "r", stdin);
	freopen("../data.out", "w", stdout);
#endif
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> N >> M >> Q;
	dir['H'][0] = 0, dir['H'][1] = 1;
	dir['V'][0] = 1, dir['V'][1] = 0;
	dir['D'][0] = 1, dir['D'][1] = 1;
	for (int i = 0; i <= 2000; i ++) C[i] = R[i] = " ";
	for (int i = 0; i <= 4100; i ++) D[i] = " ";
	for (int i = 1; i <= N; i ++){
		cin >> S[i];
		S[i] = " " + S[i];
		for (int j = 1; j <= M; j ++){
			R[i].push_back(S[i][j]);
			C[j].push_back(S[i][j]);
			D[i - j + cur].push_back(S[i][j]);
		}
	}
	__init();
	while (Q --){
		cin >> a >> b >> d1 >> x >> y >> d2;
		cout << solve() << "\n";
	}
	
	return 0;
}

详细

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

Subtask #1:

score: 11
Accepted

Test #1:

score: 11
Accepted
time: 85ms
memory: 10420kb

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: 99ms
memory: 10112kb

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: 0
Time Limit Exceeded

Test #3:

score: 0
Time Limit Exceeded

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:


Subtask #3:

score: 0
Skipped

Subtask #4:

score: 0
Skipped