ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#213433 | #2068. 最长公共前缀 | Filberte | 11 | 311ms | 76180kb | C++11 | 3.0kb | 2024-11-11 22:35:47 | 2024-11-11 23:11:13 |
answer
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll base = 131;
ll mod = 1e9 + 9;
const int N = 2050;
ll heng[N][N], shu[N][N], pw[N], xie[N][N];
char s[N][N];
int n, m, q;
ll get_heng(int i, int j, int k){
int l = j, r = j + k - 1;
// cerr << "heng:" << i << " " << j << " " << k << ":" << (shu[r][j] - shu[l - 1][k] * pw[k] % mod + mod) % mod;
// cerr << endl;
return (heng[i][r] - heng[i][l - 1] * pw[k] % mod + mod) % mod;
}
ll get_shu(int i, int j, int k){
int l = i, r = i + k - 1;
// cerr << "shu:" << i << " " << j << " " << k << ":" << shu[r][j] << " " << shu[l - 1][j] << endl;
// cerr << endl;
return (shu[r][j] - shu[l - 1][j] * pw[k] % mod + mod) % mod;
}
ll get_xie(int i, int j, int k){
return (xie[i + k - 1][j + k - 1] - xie[i - 1][j - 1] * pw[k] % mod + mod) % mod;
}
void pre_xie(int x, int y){
for(int ad = 0;x + ad <= n && y + ad <= m;ad++){
xie[x + ad][y + ad] = (xie[x + ad - 1][y + ad - 1] * base + (ll)s[x + ad][y + ad]) % mod;
}
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m >> q;
for(int i = 1;i <= n;i++) cin >> s[i] + 1;
pw[0] = 1;for(int i = 1;i <= 2010;i++) pw[i] = pw[i - 1] * base % mod;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++) heng[i][j] = (heng[i][j - 1] * base + (ll)s[i][j]) % mod;
}
// for(int i = 1;i <= n;i++){
// for(int j = 1;j <= m;j++) cerr << heng[i][j] << " ";
// cerr << endl;
// }
for(int j = 1;j <= m;j++){
for(int i = 1;i <= n;i++) shu[i][j] = (shu[i - 1][j] * base + (ll)s[i][j]) % mod;
}
// for(int i = 1;i <= n;i++){
// for(int j = 1;j <= m;j++) cerr << shu[i][j] << " ";
// cerr << endl;
// }
pre_xie(1, 1);
for(int i = 2;i <= n;i++) pre_xie(i, 1);
for(int i = 2;i <= m;i++) pre_xie(1, i);
while(q--){
int x, y, u, v;char op1, op2;
cin >> x >> y >> op1 >> u >> v >> op2;
if(s[u][v] != s[x][y]){
cout << 0 << endl;
continue;
}
int Mx;
if(op1 == 'H') Mx = m - y + 1;
else if(op1 == 'V') Mx = n - x + 1;
else Mx = min(m - y + 1, Mx = n - x + 1);
if(op2 == 'H') Mx = min(Mx, m - v + 1);
else if(op2 == 'V') Mx = min(Mx, n - u + 1);
else Mx = min({Mx, m - v + 1, Mx = n - u + 1});
int ans, l = 1, r = Mx;
//cerr << endl;
while(l <= r){
int mid = (l + r) >> 1;
ll v1, v2;
if(op1 == 'H') v1 = get_heng(x, y, mid);
else if(op1 == 'V') v1 = get_shu(x, y, mid);
else v1 = get_xie(x, y, mid);
if(op2 == 'H') v2 = get_heng(u, v, mid);
else if(op2 == 'V') v2 = get_shu(u, v, mid);
else v2 = get_xie(u, v, mid);
//cerr << "Mid = " << mid << ":" << v1 << " " << v2 << endl;
if(v1 == v2) ans = mid, l = mid + 1;
else r = mid - 1;
}
cout << ans << endl;
}
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Subtask #1:
score: 11
Accepted
Test #1:
score: 11
Accepted
time: 147ms
memory: 51352kb
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: 164ms
memory: 76180kb
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