题目描述
题解
枚举每一位,把这一位去掉,hash统计有多少个一样的
因为不存在两个串相同,所以不用管其它的直接搞就行
sort要快很多,map跑好慢…
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
using namespace std;
#define UL unsigned long long
const UL p=2000001001LL;
int n,l,ans;
char s[30005][205];
UL hash[30005],mi[205];
map <UL,int> Cnt;
int main()
{
int lalala;
scanf("%d%d%d",&n,&l,&lalala);
for (int i=1;i<=n;++i) scanf("%s",s[i]);
mi[0]=1LL;for (int i=1;i<=l;++i) mi[i]=mi[i-1]*p;
for (int i=1;i<=n;++i)
for (int j=0;j<l;++j) hash[i]+=(UL)s[i][j]*mi[l-j-1];
for (int j=0;j<l;++j)
{
Cnt.clear();
for (int i=1;i<=n;++i)
{
hash[i]-=(UL)s[i][j]*mi[l-j-1];
if (!Cnt[hash[i]]) ++Cnt[hash[i]];
else
{
int cnt=Cnt[hash[i]];
ans-=cnt*(cnt-1)/2;
++cnt;
ans+=cnt*(cnt-1)/2;
Cnt[hash[i]]=cnt;
}
hash[i]+=(UL)s[i][j]*mi[l-j-1];
}
}
printf("%d\n",ans);
}