bzoj3555 [Ctsc2014]企鹅QQ

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$一下统计答案即可。。

 

 1 //It is made by wfj_2048~
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <complex>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <cmath>
10 #include <queue>
11 #include <stack>
12 #include <map>
13 #include <set>
14 #define inf (1<<30)
15 #define il inline
16 #define RG register
17 #define ll long long
18 #define mo1 (1000000007)
19 #define mo2 (1000000009)
20 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
21 
22 using namespace std;
23 
24 struct data{ ll a,b; }st[30010];
25 
26 ll hsh1[30010][210],hsh2[30010][210],po1[210],po2[210],ans;
27 int len[30010],n,l,S;
28 char s[30010][210];
29 
30 il int gi(){
31     RG int x=0,q=1; RG char ch=getchar();
32     while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
33     if (ch=='-') q=-1,ch=getchar();
34     while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
35     return q*x;
36 }
37 
38 il int cmp(const data &x,const data &y){
39     if (x.a==y.a) return x.b<y.b; return x.a<y.a;
40 }
41 
42 il ll gethash1(RG int i,RG int l,RG int r){
43     if (l>r) return 0;
44     return (hsh1[i][r]-hsh1[i][l-1]*po1[r-l+1]+mo1)%mo1;
45 }
46 
47 il ll gethash2(RG int i,RG int l,RG int r){
48     if (l>r) return 0;
49     return (hsh2[i][r]-hsh2[i][l-1]*po2[r-l+1]+mo2)%mo2;
50 }
51 
52 il void work(){
53     n=gi(),l=gi(),S=gi(),po1[0]=po2[0]=1;
54     for (RG int i=1;i<=l;++i)
55     po1[i]=po1[i-1]*157%mo1,po2[i]=po2[i-1]*157%mo2;
56     for (RG int i=1;i<=n;++i){
57     scanf("%s",s[i]+1),len[i]=strlen(s[i]+1);
58     for (RG int j=1;j<=len[i];++j){
59         hsh1[i][j]=(hsh1[i][j-1]*157+s[i][j])%mo1;
60         hsh2[i][j]=(hsh2[i][j-1]*157+s[i][j])%mo2;
61     }
62     }
63     for (RG int i=1,top=0;i<=l;++i,top=0){
64     for (RG int j=1;j<=n;++j){
65         if (len[j]<i) continue; ++top;
66         st[top].a=(hsh1[j][i-1]*po1[len[j]-i]+gethash1(j,i+1,len[j]))%mo1;
67         st[top].b=(hsh2[j][i-1]*po2[len[j]-i]+gethash2(j,i+1,len[j]))%mo2;
68     }
69     sort(st+1,st+top+1,cmp);
70     for (RG int i=1,x=i;i<=top;++i,x=i){
71         while (i<top && st[i+1].a==st[i].a && st[i+1].b==st[i].b) ++i;
72         ans+=(ll)(i-x)*(ll)(i-x+1)>>1;
73     }
74     }
75     printf("%lld\n",ans); return;
76 }
77 
78 int main(){
79     File("qq");
80     work();
81     return 0;
82 }

 

posted @ 2017-05-05 13:56  wfj_2048  阅读(199)  评论(0编辑  收藏  举报