UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213387#2068. 最长公共前缀STASISZHY00ms0kbC++112.8kb2024-11-11 21:11:592024-11-11 23:05:36

answer

// Problem: C. 最长公共前缀
// Contest: undefined - NOIP2024训练赛 02
// URL: http://119.28.3.174/contest/1154/problem/2068
// Memory Limit: 1024 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
//#define int long long
#define fi first
#define se second
#define PII pair<int, int>

typedef unsigned long long ull;

using namespace std;

const int N = 2e3 + 10, M = 1e6 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;

int n, m, q, ans;
ull Hs[4][N][N], base = 10086001, p[N];
int a, b, c, d; 
char op1, op2;
char mp[N][N];
unordered_map<char, int> id;

inline void init()
{
	p[0] = 1;
	for(int i = 1; i <= max(n, m); i ++) p[i] = p[i - 1] * base;
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; j <= m; j ++)
			Hs[1][i][j] = Hs[1][i][j - 1] * base + (mp[i][j] - 'a' + 1);
	} 
	for(int i = 1; i <= m; i ++)
	{
		for(int j = 1; j <= n; j ++)
			Hs[2][i][j] = Hs[2][i][j - 1] * base + (mp[j][i] - 'a' + 1);
	} 
	for(int i = 1; i <= n; i ++)
	{
		for(int j = 1; i + j - 1 <= n && j <= m; j ++)
			Hs[3][i][j] = Hs[3][i][j - 1] * base + (mp[i + j - 1][j] - 'a' + 1);
	}
	for(int i = 2; i <= m; i ++)
	{
		for(int j = 1; i + j - 1 <= m && j <= n; j ++)
			Hs[3][i + n - 1][j] = Hs[3][i + n - 1][j - 1] * base + (mp[j][i + j - 1] - 'a' + 1);
	}
	id['H'] = 1, id['V'] = 2, id['D'] = 3;
}

inline ull calc(int op, int x, int l, int r)
{
	ull res = Hs[op][x][r] - Hs[op][x][l - 1] * p[r - l + 1];
	return res;
}

inline bool check(int x)
{
	ull res1, res2;
	if(id[op1] == 1) 
	{
		if(b + x - 1 > m) return false;
		res1 = calc(1, a, b, b + x - 1);
	}
	else if(id[op1] == 2) 
	{
		if(a + x - 1 > n) return false;
		res1 = calc(2, b, a, a + x - 1);
	}
	else 
	{
		if(a >= b) 
		{
			if(b + x - 1 > m) return false;
			res1 = calc(3, a - b + 1, b, b + x - 1);
		}
		else 
		{
			if(a + x - 1 > n) return false;
			res1 = calc(3, b - a + n, a, a + x - 1);
		}
	}
	if(id[op2] == 1) 
	{
		if(d + x - 1 > m) return false;
		res2 = calc(1, c, d, d + x - 1);
	}
	else if(id[op2] == 2) 
	{
		if(c + x - 1 > n) return false;
		res2 = calc(2, d, c, c + x - 1);
	}
	else 
	{
		if(c >= d) 
		{
			if(d + x - 1 > m) return false;
			res2 = calc(3, c - d + 1, d, d + x - 1);
		}
		else 
		{
			if(c + x - 1 > n) return false;
			res2 = calc(3, d - c + n, c, c + x - 1);
		}
	}
	return res1 == res2;
}

signed main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++) cin >> mp[i][j];
	init();
	while(q --)
	{
		cin >> a >> b >> op1 >> c >> d >> op2;
		int l = 1, r = max(n, m), res = 0;
		while(l <= r)
		{
			int mid = l + r >> 1;
			if(check(mid)) l = mid + 1, res = mid;
			else r = mid - 1;
		}
		cout << res << '\n';
	}
	return 0;
}

详细

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

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

1000 2000 10000
awanannwnnaaawwnwawanwnwaaaanwnaaananwawwwwnannannawwwawwaaaaannwnwnwnnaaawaawawannn...

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #3:

score: 0
Runtime Error

input:

2000 2000 1000000
gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg...

output:


result:


Subtask #3:

score: 0
Skipped

Subtask #4:

score: 0
Skipped