bzoj 3555: [Ctsc2014]企鹅QQ【hash+瞎搞】

时间:2023-03-10 04:29:45
bzoj 3555: [Ctsc2014]企鹅QQ【hash+瞎搞】

首先注意bzoj 3555: [Ctsc2014]企鹅QQ【hash+瞎搞】

先hash一下,双hash,然后枚举删去位置,把hash值排个序,把些相等的加起来统计一下对数即可

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=30005;
int ans,n,m,k;
long long h1[N][205],h2[N][205],a[N];
char s[205];
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
for(int j=1;j<=m;j++)
h1[i][j]=(h1[i][j-1]*149+s[j]);
for(int j=m;j;j--)
h2[i][j]=(h2[i][j+1]*137+s[j]);
}
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)
a[i]=(h1[i][j-1]*233+h2[i][j+1]*213);
sort(a+1,a+n+1);
int nw=1;
for(int i=2;i<=n;i++)
{
if(a[i]==a[i-1])
ans+=nw,nw++;
else
nw=1;
}
}
printf("%d\n",ans);
return 0;
}