题目

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 nnn 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 aaa and bbb as follows:

two passwords aaa and bbb are equivalent if there is a letter, that exists in both aaa and bbb;

two passwords aaa and bbb are equivalent if there is a password ccc from the list, which is equivalent to both aaa and bbb.

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.

输入

The first line contain integer n(1≤n≤2×105)n (1≤n≤2\times 10^5)n(1≤n≤2×105) — number of passwords in the list. Next n lines contains passwords from the list – non-empty strings sis_isi​, with length at most 505050letters. Some of the passwords may be equal.

It is guaranteed that the total length of all passwords does not exceed 10610^6106 letters. All of them consist only of lowercase Latin letters.

输出

In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.

题目大意

给定nnn个单词,单词全由小写字母构成,保证每个单词不超过505050个字母,总长度不超过10610^6106个字母。

两个单词是等效的,当且仅当:

  1. 两个单词中有任一字母相等。
  2. 存在一中间单词,使得两个单词都与中间单词相等。

即:

a=c,b=ca = c,b = ca=c,b=c,则有a=ba=ba=b。

思路

显然等效的单词是一个集合,容易想到用并查集维护,那么最后答案就是集合个数。

但如何维护就成了问题。

发现其实所有操作都在考虑字母,因此可以直接维护字母的并查集,而每个单词就是一个集合,因此每次读入单词时将单词内所有字母合并到一个集合中。

但还需要考虑没有出现的字母,遍历单词时记录一下即可。

复杂度O(n),n≤106O(n),n≤10^6O(n),n≤106

代码

#include <cstdio>
#include <cstring>
using namespace std;
int fa[27], vis[27];
inline int find(const int &x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
inline void unite(const int &a, const int &b)
{
int t1 = find(a), t2 = find(b);
if (t1 == t2)
return;
fa[t1] = t2;
}
char all[60];
int n, len,ans;
inline char nc()
{
static char buf[1050000],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1050000,stdin),p1==p2)?EOF:*p1++;
}
void read(char *s)
{
static char c;
for(c=nc();c>'z'||c<'a';c=nc());
for(;c>='a'&&c<='z';*(++s) = c,c=nc());
*(++s) = '\0';//快读的细节,为了防止上次读入内容影响到,要加文本终止符
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= 26; ++i)
fa[i] = i;//初始化
for (int i = 1; i <= n; ++i)
{
read(all);
len = strlen(all + 1);
for (int j = 1; j <= len; ++j)
unite(all[j] - 'a' + 1, all[1] - 'a' + 1), vis[all[j] - 'a' + 1] = 1;
}
for(int i = 1;i<=26;++i)
if(vis[i] && find(i) == i)
++ans;
printf("%d",ans);
return 0;
}

[Codeforces]1263D Secret Passwords的更多相关文章

  1. Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集

    D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...

  2. Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)

    链接: https://codeforces.com/contest/1263/problem/D 题意: One unknown hacker wants to get the admin's pa ...

  3. codeforces div2 603 D. Secret Passwords(并查集)

    题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...

  4. CodeForces 496B Secret Combination

    Secret Combination Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  5. codeforces 496B. Secret Combination 解题报告

    题目链接:http://codeforces.com/problemset/problem/496/B 题目意思:给出 n 位数你,有两种操作:1.将每一位数字加一(当某一位 > 9 时只保存个 ...

  6. codeforces 721B B. Passwords(贪心)

    题目链接: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. 【37.21%】【codeforces 721B】Passwords

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. [Codeforces] #603 (Div. 2) A-E题解

    [Codeforces]1263A Sweet Problem [Codeforces]1263B PIN Code [Codeforces]1263C Everyone is a Winner! [ ...

  9. Codeforces Round #603 (Div. 2)

    传送门 感觉脑子还是转得太慢了QAQ,一些问题老是想得很慢... A. Sweet Problem 签到. Code /* * Author: heyuhhh * Created Time: 2019 ...

随机推荐

  1. C/C++网络编程3——地址族与数据序列

    C/C++网络编程2中介绍了套接字,这一节介绍给套接字分配ip和端口号.ip用于标识一台主机,端口号用于标识一个主机中的一个应用程序,端口号占16位,0到65535,其中0到1023是知名端口号. 表 ...

  2. 「快学springboot」集成Spring Security实现鉴权功能

    Spring Security介绍 Spring Security是Spring全家桶中的处理身份和权限问题的一员.Spring Security可以根据使用者的需要定制相关的角色身份和身份所具有的权 ...

  3. 02-09Android学习进度报告九

    今天我学习了关于Adapter的基础知识,了解了Android开发的一些思路和架构. 首先我了解了Adapter的概念以及开发过程中常用的Adapter: BaseAdapter:抽象类,实际开发中我 ...

  4. 碰到的问题——建设基于TensorFlow的深度学习环境

    1.解决jupyter notebook问题:socket.error: [Errno 99] Cannot assign requested address 首先要生成一个jupyter的配置文件: ...

  5. iOS 开发之 开发一款自己的美颜相机

    以前在公司做项目时很少遇到对相机.图片进行处理的(非公司业务),只是偶尔上传,裁剪,预览下.近期自己准备写个相机应用,把图片处理的这些技术细节整理下.包含美颜相机,图片美化,简单拼图,艺术拼图等主要模 ...

  6. Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件

    一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...

  7. LeetCode 101.对称二叉树 - JavaScript

    题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / \ 2 2 / \ / \ 3 4 4 3 解法 1:递归检查 根据题目" ...

  8. ubunut18.04 下安装 gitlab ce版,使用清华源

    gitlab官方的ubuntu安装说明 https://about.gitlab.com/install/#ubuntu 该安装说明介绍的是gitlab-ee版本 按照该说明也能安装gitlab-ce ...

  9. C# FileStream 对象的Seek()方法-----转载

    原创 kevin617 发布于2010-12-08 11:22:00 阅读数 8630 收藏展开 FileStream 可以随机读写文件 使用 Seek 方法 Seek()  ----------有两 ...

  10. Django settings源码解析

    Django settings源码 Django中有两个配置文件 局部配置:配置文件settings.py,即项目同名文件夹下的settings.py文件 全局配置:django内部全局的配置文件se ...