链接:https://codeforces.com/contest/1263/problem/D
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 aa and bb as follows:
- two passwords aa and bb are equivalent if there is a letter, that exists in both aa and bb;
- two passwords aa and bb are equivalent if there is a password cc from the list, which is equivalent to both aa and bb.
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 nn (1≤n≤2⋅1051≤n≤2⋅105) — number of passwords in the list. Next nn lines contains passwords from the list – non-empty strings sisi, 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
Copy
4
a
b
ab
d
output
Copy
2
input
Copy
3
ab
bc
abc
output
Copy
1
input
Copy
1
codeforces
output
Copy
1
Note
In the second example hacker need to use any of the passwords to access the system.
题解:字符串并查集
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
#include<cstdlib>
using namespace std;
long long t,g,r;
int n,fa[50];
int vis[200001],ans;
char s[1001];
int fin(int x)
{
return x==fa[x]?x:fa[x]=fin(fa[x]);
}
int main()
{
cin>>n;
for(int i=1;i<=30;i++)
fa[i]=i;
for(int i=1;i<=n;i++)
{
cin>>s;
int l=strlen(s);
for(int j=0;j<l-1;j++)
{
vis[s[j]-'a'+1]=1;
int x=fin(s[j]-'a'+1),y=fin(s[j+1]-'a'+1);
if(x!=y)
fa[x]=y;
}
vis[s[l-1]-'a'+1]=1;
}
for(int i=1;i<=26;i++)
{
if(vis[i]&&fa[i]==i)
ans++;
}
cout<<ans;
return 0;
}

一名黑客偷窃了一份包含n个由小写字母组成的密码列表。系统中只有列表内的密码,两个密码如果存在相同的字母则等价。如果一个密码等价于另一个,那么使用它们都能登录系统。黑客需要找出最少的密码数量,以确保能肯定进入系统。输入包含密码数量n和每个密码,输出确保登录所需的最少密码数。
285

被折叠的 条评论
为什么被折叠?



