URAL 1486(二維字符串hash)

題意:一個最大500*500的字符矩陣,求最大的兩個相同的字符正方形。正方形可以有重疊部分但不能重合。


解法:首先是二分正方形的長度,然後判斷某個長度存在時候計算字符矩陣的二維hash值,二維hash的方法是:


這樣子拓展的hash算法可以O(1) 獲取任意一個子矩陣的hash值。


代碼:

/******************************************************
* @author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef unsigned long long LL;
const int Max=502;
const LL INF=100007;

char s[Max][Max];
LL hash[Max][Max];
LL hash2[Max][Max];
LL scale[Max];
LL scale2[Max];
LL seed=3131;
LL seed2=1313;
int n,m;
int count1=0;
struct edge
{
    LL data;
    int next;
    int x,y;
} edges[Max*Max];

int head[100010];
int tot=0;
void add(LL u,LL data,int x,int y)
{
    edges[tot].data=data;
    edges[tot].x=x;
    edges[tot].y=y;
    edges[tot].next=head[u];
    head[u]=tot++;
}
int have(LL value)
{
    LL t=value%INF;
    for(int i=head[t];i!=-1;i=edges[i].next)
    {
        if(edges[i].data==value)
        return i+1;
    }
    return 0;
}
bool OK(int t)
{
    memset(head,-1,sizeof head);
    tot=0;
    for(int i=1; i+t-1<=n; i++)
        for(int j=1; j+t-1<=m; j++)
        {
            LL value=0;
            value=hash2[i+t-1][j+t-1]-hash2[i+t-1][j-1]*scale[t]-hash2[i-1][j+t-1]*scale2[t]+hash2[i-1][j-1]*scale[t]*scale2[t];
            if(have(value))
            return true;
            add(value%INF,value,i,j);
        }
    return false;
}

void solve(int t)
{
    memset(head,-1,sizeof head);
    tot=0;
    for(int i=1; i+t<=n+1; i++)
        for(int j=1; j+t<=m+1; j++)
        {
            LL value=0;
            value=hash2[i+t-1][j+t-1]-hash2[i+t-1][j-1]*scale[t]-hash2[i-1][j+t-1]*scale2[t]+hash2[i-1][j-1]*scale[t]*scale2[t];
            int ok=have(value);
            if(ok)
            {
             cout<<edges[ok-1].x<<" "<<edges[ok-1].y<<endl;
             cout<<i<<" "<<j<<endl;
             return ;
            }
            else
            add(value%INF,value,i,j);
        }
}
int main()
{
    while(cin>>n>>m)
    {
        scale[0]=1;
        scale2[0]=1;
        for(int i=1; i<Max; i++)
            scale[i]=scale[i-1]*seed,scale2[i]=scale2[i-1]*seed2;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                cin>>s[i][j];
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
            {
                hash[i][j]=hash[i][j-1]*seed+s[i][j];
                hash2[i][j]=hash2[i-1][j]*seed2+hash[i][j];
            }
        int left=1;
        int right= n==m?n-1:min(n,m);
        while(left<=right)
        {
            int middle=(left+right)/2;
            if(OK(middle))
                left=middle+1;
            else
                right=middle-1;
        }
        if(right==0)
            cout<<right<<endl;
        else
        {
            cout<<right<<endl;
            solve(right);
        }
    }
    return 0;
}

發佈了217 篇原創文章 · 獲贊 10 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章