3555: [Ctsc2014]企鹅QQ 字符串哈希
枚举每一位即可
#include<bits/stdc++.h> using namespace std; int n,l,p; long long f1[30005][205],f2[30005][205],mod=10000000000007,bb=13331,ans,sum,tmp[30005]; char s[30005][205]; int main() { //freopen("xf.in","r",stdin); scanf("%d%d%d",&n,&l,&p); for(int i=1;i<=n;i++)scanf("%s",s[i]+1); for(int i=1;i<=n;i++) { f1[i][1]=s[i][1]; for(int j=2;j<=l;j++)f1[i][j]=(f1[i][j-1]*bb+s[i][j])%mod; f2[i][l]=s[i][l]; for(int j=l-1;j>=0;j--)f2[i][j]=(f2[i][j+1]*bb+s[i][j])%mod; } for(int i=1;i<=l;i++) { for(int j=1;j<=n;j++)tmp[j]=(f1[j][i-1]*233+f2[j][i+1]*101)%mod; sort(tmp+1,tmp+n+1); for(int j=1;j<=n;j++)if(tmp[j]==tmp[j-1]){sum++;ans+=sum;}else sum=0; } printf("%lld\n",ans); return 0; }