牛客练习赛1 矩阵 字符串二维hash+二分

时间:2023-03-09 23:07:11
牛客练习赛1 矩阵  字符串二维hash+二分

题目

https://ac.nowcoder.com/acm/contest/2?&headNav=www#question

解析 我们对矩阵进行二维hash,所以每个子矩阵都有一个额hash值,二分答案然后O(n^2) check 枚举矩阵终点,记录每个hash值与有两个一样的就true

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn=2e6+;
const ull base1=,base2=; //base,基数 int n, m;
char mp[][];
ull has[][];
ull p1[], p2[];
map<ull, int> mmp; void init()
{
p1[] = p2[] = ;
for(int i = ; i <= ; i ++)
{
p1[i] = p1[i-]*base1;
p2[i] = p2[i-]*base2;
}
} void Hash()
{
has[][] = ;
has[][] = ;
has[][] = ;
for(int i = ; i <= n; i ++)
{
for(int j = ; j <= m; j ++)
{
has[i][j] = has[i][j-]*base1 + mp[i][j] - 'a';
}
}
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j ++)
{
has[i][j] = has[i-][j]*base2 + has[i][j];
}
}
} bool check(int x)
{
mmp.clear();
for(int i = x; i <= n; i ++)
{
for(int j = x; j <= m; j ++)
{
ull k = has[i][j] - has[i-x][j]*p2[x] - has[i][j-x]*p1[x] + has[i-x][j-x]*p1[x]*p2[x];
mmp[k] ++;
if(mmp[k] >= )
return true;
}
}
return false;
} int main()
{
init();
while(~scanf("%d%d", &n, &m))
{
for(int i = ; i <= n; i ++)
{
scanf("%s", mp[i]+);
}
Hash();
int l = , r = , ans = ;
while(l <= r)
{
int mid = (l+r)/;
if(check(mid))
{
l = mid+;
ans = mid;
}
else
{
r = mid-;
}
}
printf("%d\n", ans);
}
return ;
}