您的位置:首页 > 其它

bzoj3555 [Ctsc2014]企鹅QQ

2017-05-05 13:56 148 查看

Description

PenguinQQ是中国最大、最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志、群、即时通讯、相册、集市等丰富强大的互联网功能体验,满足用户对社交、资讯、娱乐、交易等多方面的需求。

小Q是PenguinQQ网站的管理员,他最近在进行一项有趣的研究——哪些账户是同一个人注册的。经过长时间的分析,小Q发现同一个人注册的账户名称总是很相似的,例如Penguin1,Penguin2,Penguin3……于是小Q决定先对这种相似的情形进行统计。

小Q定义,若两个账户名称是相似的,当且仅当这两个字符串等长且恰好只有一位不同。例如“Penguin1”和“Penguin2”是相似的,但“Penguin1”和“2Penguin”不是相似的。而小Q想知道,在给定的 个账户名称中,有多少对是相似的。

为了简化你的工作,小Q给你的 个字符串长度均等于 ,且只包含大小写字母、数字、下划线以及‘@’共64种字符,而且不存在两个相同的账户名称。

Input

第一行包含三个正整数 , , 。其中 表示账户名称数量, 表示账户名称长度, 用来表示字符集规模大小,它的值只可能为2或64。

若 等于2,账户名称中只包含字符‘0’和‘1’共2种字符;

若 等于64,账户名称中可能包含大小写字母、数字、下划线以及‘@’共64种字符。

随后 行,每行一个长度为 的字符串,用来描述一个账户名称。数据保证 个字符串是两两不同的。

Output

仅一行一个正整数,表示共有多少对相似的账户名称。

Sample Input

4 3 64

Fax

fax

max

mac

Sample Output

4

HINT

4对相似的字符串分别为:Fax与fax,Fax与max,fax与max,max与mac。N<=30000,L<=200,S<=64

正解:字符串$hash$。

$CTSC$居然会有这种大水题。。

直接字符串$hash$,然后暴力枚举删掉哪一位,得出剩下的字符串$sort$一下统计答案即可。。

//It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf (1<<30)
#define il inline
#define RG register
#define ll long long
#define mo1 (1000000007)
#define mo2 (1000000009)
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)

using namespace std;

struct data{ ll a,b; }st[30010];

ll hsh1[30010][210],hsh2[30010][210],po1[210],po2[210],ans;
int len[30010],n,l,S;
char s[30010][210];

il int gi(){
RG int x=0,q=1; RG char ch=getchar();
while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
if (ch=='-') q=-1,ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
return q*x;
}

il int cmp(const data &x,const data &y){
if (x.a==y.a) return x.b<y.b; return x.a<y.a;
}

il ll gethash1(RG int i,RG int l,RG int r){
if (l>r) return 0;
return (hsh1[i][r]-hsh1[i][l-1]*po1[r-l+1]+mo1)%mo1;
}

il ll gethash2(RG int i,RG int l,RG int r){
if (l>r) return 0;
return (hsh2[i][r]-hsh2[i][l-1]*po2[r-l+1]+mo2)%mo2;
}

il void work(){
n=gi(),l=gi(),S=gi(),po1[0]=po2[0]=1;
for (RG int i=1;i<=l;++i)
po1[i]=po1[i-1]*157%mo1,po2[i]=po2[i-1]*157%mo2;
for (RG int i=1;i<=n;++i){
scanf("%s",s[i]+1),len[i]=strlen(s[i]+1);
for (RG int j=1;j<=len[i];++j){
hsh1[i][j]=(hsh1[i][j-1]*157+s[i][j])%mo1;
hsh2[i][j]=(hsh2[i][j-1]*157+s[i][j])%mo2;
}
}
for (RG int i=1,top=0;i<=l;++i,top=0){
for (RG int j=1;j<=n;++j){
if (len[j]<i) continue; ++top;
st[top].a=(hsh1[j][i-1]*po1[len[j]-i]+gethash1(j,i+1,len[j]))%mo1;
st[top].b=(hsh2[j][i-1]*po2[len[j]-i]+gethash2(j,i+1,len[j]))%mo2;
}
sort(st+1,st+top+1,cmp);
for (RG int i=1,x=i;i<=top;++i,x=i){
while (i<top && st[i+1].a==st[i].a && st[i+1].b==st[i].b) ++i;
ans+=(ll)(i-x)*(ll)(i-x+1)>>1;
}
}
printf("%lld\n",ans); return;
}

int main(){
File("qq");
work();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: