牛客 矩阵 字符串二维哈希

https://ac.nowcoder.com/acm/problem/13610

题意中文的,很简单

做法,二分加哈希,关于二维字符串怎么哈希,公式可以去看看这个

https://blog.csdn.net/KXL5180/article/details/90447007

还有需要注意的就是题目他卡了map 要用 unordered_map才能过

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ull hs1=200379;
const ull hs2=196613;
const ll mod=1e9+7;
const int N=1010;
ull p1[N],p2[N];
ull hs[N][N];
int m,n;
char s[N][N];
void init()
{
    p1[0]=p2[0]=1;
    for(int i=1;i<N;i++)
        p1[i]=p1[i-1]*hs1;
    for(int i=1;i<N;i++)
        p2[i]=p2[i-1]*hs2;
}
int judge(int x)
{
    ull cnt;
    unordered_map<ull,int>mp;
    for(int i=x;i<=n;i++)
    {
        for(int j=x;j<=m;j++)
        {
            cnt=hs[i][j]-hs[i-x][j]*p1[x]-hs[i][j-x]*p2[x]+hs[i-x][j-x]*p1[x]*p2[x];
            mp[cnt]++;
            if(mp[cnt]==2)
                return 1;
        }
    }
    return 0;
}
int main()
{
    init();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            hs[i][j]=hs[i][j-1]*hs2+hs[i-1][j]*hs1-hs[i-1][j-1]*hs1*hs2+s[i][j];
        }
    }
    int l=0,r=min(n,m),mid,ans;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(judge(mid))
        {
            ans=mid;
            l=mid+1;
        }
        else
            r=mid-1;
    }
    printf("%d\n",ans);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值