社区讨论

一个小细节

P10474[ICPC-Beijing 2011] Matrix 矩阵哈希参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lzj54xtb
此快照首次捕获于
2024/08/07 08:58
2 年前
此快照最后确认于
2024/08/07 09:58
2 年前
查看原帖
题目中约定了A100A ≤ 100,但没有约定B100B≤100 .
另外我Case #4 T了,另外十个点都是50ms左右,有无懂的佬指点一下。
CPP
#include <iostream>
#include <cstdio>
#define ull unsigned long long
using namespace std;
const ull P = 131, Q = 13331;
int m, n, u, v, T;
ull Hash[1010][1010], Power[1010] = {1}, Qower[1010] = {1};
ull a[1010][1010], b[1010][1010];
char ch;

ull gethash(int x1, int y1, int x2, int y2){
    return Hash[x2][y2] - Hash[x1-1][y2]*Qower[x2-x1+1] - Hash[x2][y1-1]*Power[y2-y1+1] + Hash[x1-1][y1-1]*Power[y2-y1+1]*Qower[x2-x1+1];
}
int check(){
    for(int i=u; i<=m; i++){
        for(int j=v; j<=n; j++){
            if(gethash(i-u+1, j-v+1, i, j) == b[u][v]) return 1;
        }
    }
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    freopen("P10474.txt", "r", stdin);
    cin >> m >> n >> u >> v;
    for(int i=1; i<=1000; i++){
        Power[i] = Power[i-1] * P;
        Qower[i] = Qower[i-1] * Q;
    }
    for(int i=1; i<=m; i++){
        for(int j=1; j<=n; j++){
            cin >> ch;
            a[i][j] = ch - '0';
            Hash[i][j] = Hash[i][j-1] * P + a[i][j];
        }
    }
    for(int j=1; j<=n; j++){
        for(int i=1; i<=m; i++){
            Hash[i][j] = Hash[i-1][j] * Q + Hash[i][j];
        }
    }
    cin >> T;
    while(T--){
        for(int i=1; i<=u; i++){
            for(int j=1; j<=v; j++){
                cin >> ch;
                b[i][j] = ch - '0';
                b[i][j] = b[i][j-1] * P + b[i][j];
            }
        }
        for(int j=1; j<=v; j++){
            for(int i=1; i<=u; i++){
                b[i][j] = b[i-1][j] * Q + b[i][j];
            }
        }
        cout << check() << endl;
    }
    return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...