[Codeforces]1263D 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 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个字母。
两个单词是等效的,当且仅当:
- 两个单词中有任一字母相等。
- 存在一中间单词,使得两个单词都与中间单词相等。
即:
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的更多相关文章
- 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, ...
- 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 ...
- codeforces div2 603 D. Secret Passwords(并查集)
题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...
- CodeForces 496B Secret Combination
Secret Combination Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- codeforces 496B. Secret Combination 解题报告
题目链接:http://codeforces.com/problemset/problem/496/B 题目意思:给出 n 位数你,有两种操作:1.将每一位数字加一(当某一位 > 9 时只保存个 ...
- codeforces 721B B. Passwords(贪心)
题目链接: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【37.21%】【codeforces 721B】Passwords
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [Codeforces] #603 (Div. 2) A-E题解
[Codeforces]1263A Sweet Problem [Codeforces]1263B PIN Code [Codeforces]1263C Everyone is a Winner! [ ...
- Codeforces Round #603 (Div. 2)
传送门 感觉脑子还是转得太慢了QAQ,一些问题老是想得很慢... A. Sweet Problem 签到. Code /* * Author: heyuhhh * Created Time: 2019 ...
随机推荐
- AbstractQueuedSynchronizer AQS源码分析
申明:jdk版本为1.8 AbstractQueuedSynchronizer是jdk中实现锁的一个抽象类,有排他和共享两种模式. 我们这里先看排他模式,共享模式后面结合java.util.concu ...
- springmvc_ajax异步上传文件(基于ajaxfileupload.js)
引入js <script th:src="@{/js/ajaxfileupload.js}"></script> html <tr> <t ...
- Layui我提交表单时,table.reload(),表格会请求2次,是为什么?
重载两次是因为搜索按钮用的是button 改成<a class="layui-btn" data-type="reload">搜索</a> ...
- c# 分页 PaginatedList<TResult>
using System; using System.Collections.Generic; using System.Linq; namespace Microestc.PaginatedList ...
- 吴裕雄--天生自然ORACLE数据库学习笔记:Oracle系统调优
--修改 alter system set large_pool_size=64m; --显示 show parameter large_pool_size; select sum(getmisses ...
- C++代码书写规范——给新手程序员的一些建议
代码就是程序员的面子,无论是在工作中在电脑上写程序代码还是在面试时在纸上写演示代码我们都希望写出整洁,优雅的代码.特别在工作中当我们碰到需要维护别人的代码,或者是多人参与一个项目大家一起写代码的时候, ...
- Activity切换动画。从右边滑入,关闭时从左边滑入
直接贴代码吧 1. 动画文件(两个动画文件配置到res/anim目录下) activity_anim_in_right.xml <?xml version="1.0" e ...
- Mongodb - 解决 ( aggregate聚合管道 ) $match 根据 id 匹配 返回 [ ] 的问题
需要对 id 进行转换 const mongoose = require('mongoose') var ObjectId = mongoose.Types.ObjectId; await Use ...
- table左边固定-底部横向滚动条-demo
图: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
- MySQL必知必会(1-8)章
1.数据库,表,列,行,模式,每一列有唯一的数据类型,模式是数据库和表的布局及特性 2.满足主键的两个条件:任意两行都不具有相同的主键值,每行都必须具有主键值 3.SQL(Structured Que ...