UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213350#2068. 最长公共前缀KXG0953ms51572kbC++112.1kb2024-11-11 19:29:122024-11-11 23:02:08

answer

#include <cstdio>
#include <algorithm>
using namespace std;
const int B = 31;
const int mod = 998244853;
int n, m, q, powB[2010], a, b, x, y;
char s[2010][2010];
char opt1[20], opt2[20];
int Hh[2010][2010], Hv[2010][2010], Hd[2010][2010];
int decode(char c) {
    return c - 'a';
}
int Hash(int x, int y, int len, char dir) {
    if (dir == 'H') {
        return (Hh[x][y + len - 1] - 1ll * Hh[x][y - 1] * powB[len] % mod + mod) % mod;
    } else if (dir == 'V') {
        return (Hv[x + len - 1][y] - 1ll * Hv[x - 1][y] * powB[len] % mod + mod) % mod;
    } else if (dir == 'D') {
        return (Hd[x + len - 1][y + len - 1] - 1ll * Hd[x - 1][y - 1] * powB[len] % mod + mod) % mod;
    }
}
int getlen(int x, int y, char dir) {
    if (dir == 'H') {
        return m - y + 1;
    } else if (dir == 'V') {
        return n - x + 1;
    } else {
        return min(n - x + 1, m - y + 1);
    }
}
int main() {
    powB[0] = 1;
    for (int i = 1; i <= 2000; i++) {
        powB[i] = powB[i - 1] * B % mod;
    }
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; i++) {
        scanf("%s", s[i] + 1);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            Hh[i][j] = (1ll * Hh[i][j - 1] * B + decode(s[i][j])) % mod;
        }
    }
    for (int j = 1; j <= m; j++) {
        for (int i = 1; i <= n; i++) {
            Hv[i][j] = (1ll * Hv[i - 1][j] * B + decode(s[i][j])) % mod;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            Hd[i][j] = (1ll * Hd[i - 1][j - 1] * B + decode(s[i][j])) % mod;
        }
    }
    while (q--) {
        scanf("%d%d%s%d%d%s", &a, &b, opt1, &x, &y, opt2);
        int l = 1, r = min(getlen(a, b, opt1[0]), getlen(x, y, opt2[0])), ans = 0;
        while (l <= r) {
            int mid = (l + r) >> 1;
            if (Hash(a, b, mid, opt1[0]) == Hash(x, y, mid, opt2[0])) {
                l = mid + 1;
                ans = mid;
            } else {
                r = mid - 1;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

详细

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

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 51ms
memory: 26056kb

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:

wrong answer 2913th words differ - expected: '7', found: '6'

Subtask #2:

score: 0
Wrong Answer

Test #3:

score: 0
Wrong Answer
time: 902ms
memory: 51572kb

input:

2000 2000 1000000
gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg...

output:

6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
...

result:

wrong answer 1st words differ - expected: '1146', found: '6'

Subtask #3:

score: 0
Skipped

Subtask #4:

score: 0
Skipped