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, 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个密码,但里面有些密码是等价的,等价的定义是:
假设存在一个字母x,在a和b字符串都出现过,那么a字符串和b字符串就是等价的。
假设a字符串和c字符串等价,b和c字符串等价,那么a和b也等价。
问你最少掌握多少个密码,就能掌握所有密码了
题解
视频题解 https://www.bilibili.com/video/av77514280/
并查集裸题。。。每次和自己所包含的字母合成一坨即可
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+7;
int fa[maxn],n;
string s[maxn];
int fi(int x){
return fa[x]==x?fa[x]:fa[x]=fi(fa[x]);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i];
}
for(int j=1;j<=n+26;j++){
fa[j]=j;
}
for(int i=1;i<=n;i++){
for(int j=0;j<s[i].size();j++){
fa[fi(i)]=fa[fi(n+s[i][j]-'a'+1)];
}
}
int ans = 0;
set<int>vis;
for(int i=1;i<=n;i++){
if(!vis.count(fi(i))){
ans++;
vis.insert(fi(i));
}
}
cout<<ans<<endl;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2015-11-30 Uva 3767 Dynamic len(set(a[L:R])) 树套树
2015-11-30 cdoj 1246 每周一题 拆拆拆~ 分解质因数
2014-11-30 wikioi 1380 没有上司的舞会 树形dp
2014-11-30 wikioi 1434 孪生素数 水题、素数模版
2014-11-30 NOIP 2002提高组 选数 dfs/暴力
2014-11-30 Codeforces Round 486C - Palindrome Transformation 贪心