Secret Passwords

Secret Passwords

One unknown hacker wants to get the admin’s password of AtForces testing system, to get problems from the next contest. To achieve that, he sneaked into the administrator’s office and stole a piece of paper with a list of nn passwords — strings, consists of small Latin letters.

Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords a and b as follows:

  • two passwords a and b are equivalent if there is a letter, that exists in both a and b;
  • two passwords a and b are equivalent if there is a password cc from the list, which is equivalent to both a and b.

If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.

For example, if the list contain passwords “a”, “b”, “ab”, “d”, then passwords “a”, “b”, “ab” are equivalent to each other, but the password “d” is not equivalent to any other password from list. In other words, if:

  • admin’s password is “b”, then you can access to system by using any of this passwords: “a”, “b”, “ab”;
  • admin’s password is “d”, then you can access to system by using only “d”.

Only one password from the list is the admin’s password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.

Input

The first line contain integer n(1≤n≤2⋅10) — number of passwords in the list. Next n lines contains passwords from the list – non-empty strings si, with length at most 5050 letters. Some of the passwords may be equal.

It is guaranteed that the total length of all passwords does not exceed 106106 letters. All of them consist only of lowercase Latin letters.

Output

In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.

Examples

Input

1
2
3
4
5
4
a
b
ab
d

Output

1
2

Input

1
2
3
4
3
ab
bc
abc

Output

1
1

Input

1
2
1
codeforces

Output

1
1

Note

In the second example hacker need to use any of the passwords to access the system.

题解:

利用并查集,把出现在同一个单词的字母直接合并给一个祖先,然后最后判断有几个祖先即可!

例如:abc和ade;第一个abc是一个集合,祖先是c,第二个因为有a所以他们的祖先都可以传递到e,即a-b-c-d-e。

实现代码:

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
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+9;
int n,vis[maxn],pre[maxn],ans;
string a[maxn];
int find(int x){
int r=x;
while(pre[r]!=r)
r=pre[r];
return r;
}
void join(int q,int w){
int fx,fy;
fx=find(q);
fy=find(w);
if(fx!=fy)
pre[fx]=fy;
}
int main()
{
cin>>n;
for(int i=0;i<=30;i++) pre[i]=i;
for(int i=1;i<=n;i++)
{
cin>>a[i];
int b[27]={0};
for(int l=a[i].length(),j=0;j<l;j++)
{
int s=a[i][j]-'a'+1;//利用ascii码把字母转换为数字
b[s]++;vis[s]++;//b数组用来判断祖先,vis数组最后用来找祖先
}
for(int q=1;q<=26;q++)
{
if(b[q]==0)
continue;
for(int j=q+1;j<=26;j++)
{
if(b[j]==0)
continue;
join(q,j);//合并祖先
}
break;
}
}
for(int i=1;i<=26;i++)
if(vis[i]&&find(i)==i)//寻找祖先
ans++;
cout<<ans;
}