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
Input
4
a
b
ab
d
Output
2
Input
3
ab
bc
abc
Output
1
Input
1
codeforces
Output
1
Note
In the second example hacker need to use any of the passwords to access the system.
思路:密码相同有两种规则,如果含有相同的字母或者同时和另一个密码相同。那么我们就把这n个字符串中的字母,出现在同一字母中的利用并查集更新为一个集合。最后查找出现的这几个字母中,有几个祖先就可以了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=30;
int f[maxx];
string s;
int n,len;inline void init()
{for(int i=0;i<=26;i++) f[i]=i;
}
inline int getf(int u)
{return u==f[u]?u:f[u]=getf(f[u]);
}
inline void merge(int u,int v)
{int t1=getf(u);int t2=getf(v);if(t1!=t2) f[t1]=t2;
}
int main()
{scanf("%d",&n);init();set<int> se;for(int i=1;i<=n;i++){cin>>s;len=s.length();for(int j=1;j<len;j++) merge(s[j]-'a',s[j-1]-'a');for(int j=0;j<len;j++) se.insert(s[j]-'a');}map<int,bool> mp;int ans=0;for(set<int> ::iterator it=se.begin();it!=se.end();it++){int zz=getf(*it);if(!mp[zz]) mp[zz]=1,ans++;}cout<<ans<<endl;return 0;
}

努力加油a啊,(o)/~

Secret Passwords CodeForces - 1263D(并查集)相关推荐

  1. A - Cthulhu CodeForces - 103B (并查集)

    -Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for th ...

  2. Codeforces 1263D(Secret Passwords )

    题目: One unknown hacker wants to get the admin's password of AtForces testing system, to get problems ...

  3. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集

    C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...

  4. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  5. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  6. Codeforces Global Round 14 F. Phoenix and Earthquake 思维 + 并查集

    传送门 文章目录 题意: 思路: 题意: 给你nnn个点,mmm条边,限制xxx,每个点都有沥青aia_iai​,定义合并两个点即两点之间有边且au+av≥xa_u+a_v\ge xau​+av​≥x ...

  7. Codeforces 699D Fix a Tree 并查集

    原题:http://codeforces.com/contest/699/problem/D 题目中所描述的从属关系,可以看作是一个一个块,可以用并查集来维护这个森林.这些从属关系中会有两种环,第一种 ...

  8. Codeforces Round #548 (Div. 2) C. Edgy Trees(dfs || 并查集)

    题目链接:https://codeforces.com/contest/1139/problem/C 题意:给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路 ...

  9. C. Edgy Trees---(思维题+并查集的运用)---Codeforces Round #548 (Div. 2)

    Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes 题目链接http://codeforces.c ...

最新文章

  1. 龙将加速浏览器_《看门狗:军团》即将开启预载,迅游加速器支持下载和联机加速 18183手机游戏网...
  2. 有关Spring注解@xxx的零碎知识
  3. extjs弹出窗口查看文本内容-new Ext.Window
  4. 文献读的越多,离原创越远
  5. topcoder srm 698 div1 -3
  6. 指针作为函数参数 进行内存释放 并置NULL
  7. ZT: Delphi编程规范1.0.0.0 (yckxzjj )
  8. submit和button的区别
  9. python绘图背景透明_如何在 Matplotlib 中更改绘图背景
  10. html5之Fullscreen全屏API
  11. 【xxl-job源码篇01】xxl-job源码解读 神奇的时间轮 触发流程解读
  12. vba excel 画折线图
  13. 87.3 laravel中常见问题以及解决方案
  14. JAVA简单项目购物系统的整个开发过程详解(内含源码和注释)
  15. 表达式的LenB(123程序设计ABC)的值是27吗
  16. 如何有效实现软件的需求管理 - 1
  17. 气候变化如何影响致命的龙卷风?
  18. [Simulink] 基于模型的测试与验证学习笔记_Step 4:Testing By Simulation
  19. ie下“无法将这个证书验证到一个受信赖的证书颁发机构”问题解决
  20. mysql在哪里安装_mysql安装路径在哪里

热门文章

  1. python选取元音开头的单词_一学生易错词汇aan的选择元音字母开头的单词用an辅音字母...
  2. extjs获取元素name属性值_【ExtJS】各种获取元素组件方法
  3. No slave process to process jobs, aborting 报错!!!
  4. 人机协作机器人发展趋势_移动机器人:人机协作是未来的发展趋势
  5. Android开发之拍照后图片旋转的问题
  6. linux 输入是否为数字,【shell】Linux shell 之 判断用户输入的变量是否为数字
  7. python基本输入输出系统_Python的输入输出
  8. eclipse 关闭时progress information弹框_如何关闭 iPhone 中的评分和好评弹窗?
  9. 04Hadoop中的setPartitionerClass/SortComparator/GroupingComparator问题
  10. @Mock与@InjectMocks的区别