1 solutions

  • 1
    @ 2025-10-30 10:09:18

    考虑二维哈希,维护正的哈希和反的哈希。对于一个矩形,如果他的正哈希值等于反哈希值,那符合题意

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    const int N = 333,mod = 998244353,p1 = 19491001,p2 = 97384513;
    int n,m,a[N][N],hash1[N][N],hash2[N][N];
    
    inline void h1(int x,int y){
        hash1[x][y] = (hash1[x-1][y]*p1%mod + hash1[x][y-1]*p2%mod - hash1[x-1][y-1]*p1%mod*p2%mod + a[x][y] + mod) % mod;
    }
    
    inline void h2(int x,int y){
        hash2[x][y] = (hash2[x+1][y]*p1%mod + hash2[x][y+1]*p2%mod - hash2[x+1][y+1]*p1%mod*p2%mod + a[x][y] + mod) % mod;
    }
    
    inline int fpow(int a,int b){
        int res = 1;
        while(b){
            if(b & 1) res = res * a % mod;
            a = a * a % mod;
            b >>= 1;
        }
        return res;
    }
    
    inline bool check(int x){
        for(int i=1;i+x-1<=n;i++){
            for(int j=1;j+x-1<=m;j++){
                int hsh = (hash1[i+x-1][j+x-1] - hash1[i-1][j+x-1]*fpow(p1,x)%mod - hash1[i+x-1][j-1]*fpow(p2,x)%mod + hash1[i-1][j-1]*fpow(p1,x)%mod*fpow(p2,x)%mod + mod*2) % mod;
                int hsh1 = (hash2[i][j] - hash2[i+x][j]*fpow(p1,x)%mod - hash2[i][j+x]*fpow(p2,x)%mod + hash2[i+x][j+x]*fpow(p1,x)%mod*fpow(p2,x)%mod + mod*2) % mod;
                if(hsh == hsh1) return true;
            }
        }
        return false;
    }
    
    signed main() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        cin >> n >> m;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                char ch;
                cin >> ch;
                if(ch=='1') a[i][j] = 1;
            }
        }    
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                h1(i,j);
            }
        }
        for(int i=n;i>=1;i--){
            for(int j=m;j>=1;j--){
                h2(i,j);
            }
        }
        int res = 0;
        for(int len=1;len<=min(n,m);len++){
            if(check(len)) res = len;
        }
        if(res<=1) cout << -1;
        else cout << res;
        return 0;
    }
    

    Information

    ID
    8406
    Time
    1000ms
    Memory
    32MiB
    Difficulty
    5
    Tags
    # Submissions
    1
    Accepted
    1
    Uploaded By