本文共 2620 字,大约阅读时间需要 8 分钟。
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 n 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:
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:
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⋅105) — number of passwords in the list. Next n lines contains passwords from the list – non-empty strings si, with length at most 50 letters. Some of the passwords may be equal.
It is guaranteed that the total length of all passwords does not exceed 106 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.
Sample Input
4
a b ab dSample Output
2
AC代码:
#includeusing namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'int pre[26];bool vis[26];void init(){ for(int i=0;i<26;i++) pre[i]=i;}int _find(int x){ if(pre[x]==x) return x; return pre[x]=_find(pre[x]);}void merge(int x,int y){ x=_find(x); y=_find(y); if(x==y) return; pre[y]=x;}int main(){ SIS; int n,ans=0; string s; init(); cin >> n; for(int t=0;t > s; int len = s.size(); vis[s[0]-'a']=true; for(int i=1;i
转载地址:http://iiyof.baihongyu.com/