D. 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 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:

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 c 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⋅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.

Examples
inputCopy
4
a
b
ab
d
outputCopy
2
inputCopy
3
ab
bc
abc
outputCopy
1
inputCopy
1
codeforces
outputCopy
1
Note
In the second example hacker need to use any of the passwords to access the system.

//巧用并查集,编号与字符合拢在一起。最后判断有几个合拢的


#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7;
int f[N];
string s[N];
int n;
int find(int x)
{
  return f[x]==x?f[x]:f[x]=find(f[x]);
}
int main()
{
  cin>>n;
  for(int i=1;i<=n;i++)  cin>>s[i];
  for(int i=1;i<=n+26;i++) f[i]=i;
  for(int i=1;i<=n;i++)
  {
    for(int j=0;j<s[i].size();j++)
    {
      f[find(i)]=f[find(n+s[i][j]-'a'+1)];
    }
  }
  set<int> v;
  int ans=0;
  for(int i=1;i<=n;i++)
  {
    if(!v.count(find(i)))
    {
      ans++;
      v.insert(find(i));
    }
  }
  cout<<ans<<endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值