zoukankan      html  css  js  c++  java
  • 【URAL 1486】Equal Squares(二维哈希+二分)

    Description


    During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued about who of them could in 300 minutes find a pair of equal squares of the maximal size in a matrix of size N × M containing lowercase English letters. Squares could overlap each other but could not coincide. He who had found a pair of greater size won. Petr walked by, looked at the matrix, said that the optimal pair of squares had sides K, and walked on. Vova and Sasha still cannot find this pair. Can you help them?

    Input


    The first line contains integers N and M separated with a space. 1 ≤ N, M ≤ 500. In the next N lines there is a matrix consisting of lowercase English letters, M symbols per line.
    Output
    In the first line, output the integer K which Petr said. In the next two lines, give coordinates of upper left corners of maximal equal squares. If there exist more than one pair of equal squares of size K, than you may output any of them. The upper left cell of the matrix has coordinates (1, 1), and the lower right cell has coordinates (N, M). If there are no equal squares in the matrix, then output 0.

    Sample input

    5 10
    ljkfghdfas
    isdfjksiye
    pgljkijlgp
    eyisdafdsi
    lnpglkfkjl
    

    Sample output

    3
    1 1
    3 3
    

    题解


    二分答案,然后利用二维哈希(O(n^2))check,总的时间为(O(n^2 logn))

    参考代码

    #include <map>
    #include <queue>
    #include <cstdio>
    #include <complex>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #define ll unsigned long long
    #define inf 1000000000
    #define PI acos(-1)
    #define bug puts("here")
    #define REP(i,x,n) for(int i=x;i<=n;i++)
    #define DEP(i,n,x) for(int i=n;i>=x;i--)
    #define mem(a,x) memset(a,x,sizeof(a))
    using namespace std;
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void Out(int a){
        if(a<0) putchar('-'),a=-a;
        if(a>=10) Out(a/10);
        putchar(a%10+'0');
    }
    const int N=505;
    char a[N][N];
    ll ha[N][N];
    ll p1[N],p2[N];
    map<ll,int>vis;
    int ansx1,ansy1,ansx2,ansy2;
    int n,m;
    struct node{
       int x,y;
    }book[N*N];
    bool check(int s){
        vis.clear();
        ll tmp;int tot=0;
        REP(i,s,n) REP(j,s,m){
            tmp=ha[i][j]-ha[i-s][j]*p2[s]-ha[i][j-s]*p1[s]+ha[i-s][j-s]
            *p1[s]*p2[s];
            if(vis[tmp]){
                int id=vis[tmp];
                ansx1=book[id].x;ansy1=book[id].y;
                ansx2=i-s+1;ansy2=j-s+1;
                return true;
            }
            ++tot;
            book[tot].x=i-s+1;book[tot].y=j-s+1;
            vis[tmp]=tot;
        }
        return false;
    }
    int main(){
        cin>>n>>m;
        REP(i,1,n) cin>>(a[i]+1);
        int seed1=123,seed2=1789;
        REP(i,1,n) REP(j,1,m) ha[i][j]=ha[i][j-1]*seed1+a[i][j];
        REP(i,1,n) REP(j,1,m) ha[i][j]=ha[i-1][j]*seed2+ha[i][j];
        p1[0]=p2[0]=1;
        REP(i,1,m) p1[i]=p1[i-1]*seed1;
        REP(i,1,n) p2[i]=p2[i-1]*seed2;
        int l=1,r=min(n,m),ans=-1;
        while(l<=r){
            int mid=(l+r)>>1;
            if(check(mid)){
                l=mid+1;
                ans=mid;
            }else r=mid-1;
        }
        if(ans!=-1){
            printf("%d
    ",ans);
            printf("%d %d
    %d %d
    ",ansx1,ansy1,ansx2,ansy2);
        }else puts("0");
        return 0;
    }
    
  • 相关阅读:
    揭秘:如何为 Kubernetes 实现原地升级
    阿里云叔同:以容器为代表的云原生技术,已经成为释放云价值的最短路径
    如何画好一张架构图?(内含知识图谱)
    K8s 资源全汇总 | K8s 大咖带你 31 堂课从零入门 K8s
    15-16年总结——拨开云雾终见青天
    从一个程序员的角度看——微信小应用(第一弹 初学)
    h5直播开发之旅总结
    初探和实现websocket心跳重连(npm: websocket-heartbeat-js)
    组件化h5活动模板的实现
    总结JavaScript事件机制
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/7532088.html
Copyright © 2011-2022 走看看