博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CodeForces 1263D --- Secret Passwords】并查集
阅读量:2038 次
发布时间:2019-04-28

本文共 2620 字,大约阅读时间需要 8 分钟。

【CodeForces 1263D --- Secret Passwords】并查集

Description

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.

Sample Input

4

a
b
ab
d

Sample Output

2

AC代码:

#include 
using 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/

你可能感兴趣的文章
jedis : NoSuchMethodError: org.springframework.util.Assert.isTrue(ZLjava/util/function/Supplier
查看>>
常见JedisConnectionException异常分析
查看>>
linux下常见命令
查看>>
loadrunner通过注册中心 网关压测spring cloud应用
查看>>
Spring Cloud 异常处理
查看>>
Redis集群性能测试工具redis-benchmark
查看>>
ActiveMQ 数据持久化
查看>>
RocketMQ批量消费、消息重试、消费模式、刷盘方式
查看>>
redis中与清空数据有关的命令
查看>>
redis cluster 一个问题:双master不能在一个虚拟机/物理机上
查看>>
Redis缓冲区设置
查看>>
RocketMQ初步应用架构理论(主从切换/异/同步刷盘)
查看>>
eclipse 修改 项目的git地址
查看>>
Spring gateway使用一个lambda例子的说明
查看>>
Bug: Return value of putIfAbsent is ignored, but list is reused
查看>>
分析验证zuul支持做外部网关
查看>>
Hive 快速入门(全面)
查看>>
修改linux最大文件句柄数
查看>>
RocketMQ 自定义(日志)文件路径
查看>>
maven配置环境变量
查看>>