【BZOJ3555】 [Ctsc2014]企鹅QQ
BZOJ3555 [Ctsc2014]企鹅QQ
Solution
只需要前缀Hash,然后考虑每一段的贡献就好了!!!
代码实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
#define ull unsigned long long
const int N=30010,L=210;
const ull Base=19260817,Base1=19491001,Base2=23333;
ull Hash[N][L],tmp[N],pw[L];
int n,l,S;
ll ans;
char s[N];
void hAsh(int id)
{
for(int i=1;i<=l;i++)
Hash[id][i]=Hash[id][i-1]*Base+s[i];
}
ull getHash(int id,int l,int r)
{
if(l>r)return 0;
return Hash[id][r]-Hash[id][l-1]*pw[r-l+1];
}
int main()
{
n=gi();l=gi();S=gi();
pw[0]=1;
for(int i=1;i<=l;i++)
pw[i]=pw[i-1]*Base;
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
hAsh(i);
}
for(int j=1;j<=l;j++)
{
for(int i=1;i<=n;i++)tmp[i]=Hash[i][j-1]*pw[l-j]+getHash(i,j+1,l);
sort(tmp+1,tmp+n+1);
int now=1;
for(int i=2;i<=n;i++)
{
if(tmp[i]==tmp[i-1])ans+=now,now++;
else now=1;
}
}
printf("%lld\n",ans);
return 0;
}