-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUVA11732.cpp
More file actions
67 lines (60 loc) · 1.27 KB
/
UVA11732.cpp
File metadata and controls
67 lines (60 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
#define maxn 4000010
#define size 63
using namespace std;
int ch[maxn][size];
int val[maxn],sz;
int fa[maxn];
void init(){
memset(ch[0],0,sizeof(ch[0]));
val[0]=0;
sz=1;
}
int idx(char c){
if(!c) return c;
else if('a'<=c&&c<='z') return c-'a'+1;
else if('A'<=c&&c<='Z') return c-'A'+27;
else return c-'0'+53;
}
void insert(char* s){
int u=0;
for(int i=0;;i++){
val[u]++;
if(s[i]==-1) break;
int id=idx(s[i]);
if(!ch[u][id]){
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
ch[u][id]=sz++;
}
fa[ch[u][id]]=u;
u=ch[u][id];
}
}
int N;
char str[1010];
int kase;
long long ans;
void dfs(int u){
if(u){
ans+=1ll*(val[fa[u]]-val[u])*val[u];
ans+=2ll*val[u]*(val[u]-1);
}
for(int i=0;i<size;i++)
if(ch[u][i]) dfs(ch[u][i]);
}
int main(){
while(scanf("%d",&N)==1&&N){
init();
while(N--){
scanf("%s",str);
int l=strlen(str);
str[l+1]=-1;
insert(str);
}
printf("Case %d: ",++kase);
ans=0; dfs(0);
printf("%lld\n",ans/2);
}
return 0;
}//