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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值