codeforces div2 603 D. Secret Passwords(并查集)
题目链接:https://codeforces.com/contest/1263/problem/D
题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个集合,可以互相破解,先求有多少个不同集合的密码
思路:简单的并查集。首先一共有26个字母,对于每个密码,我们合并其所涵盖的字母,例如密码1:abcd,那么abcd合并起来,含有abcd的任意密码都同属于一个集合,所有我们把n个密码串先做一次合并。最终再扫一遍所有的密码串,用set维护集合的个数,最终输出set集合的大小即可。
代码:
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn =2e5+;
int fa[];
void init(){
for(int i = ;i<=;i++){
fa[i] = i;
}
}
int find(int x){
if(fa[x] == x) return x;
else return fa[x] = find(fa[x]);
}
void unite(int x,int y){
int tx = find(x),ty = find(y);
if(tx == ty) return ;
fa[y] = tx;
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin>>n;
init();
set<int> ss;
vector<string> v;
while(n--){
string s;
cin>>s;
v.push_back(s);
int t = s[]-'a'+;
for(int i = ;i<s.length() ;i++){
int cur = s[i]-'a'+;
unite(t,cur);//合并当前字符串的所有字母
}
}
for(int i = ;i<v.size() ;i++){
for(int j =;j<v[i].length();j++){
ss.insert(find(v[i][j]-'a'+));//扫所有字符串的所有字母,判断是否是同一父亲
}
}
cout<<ss.size();
return ;
}
codeforces div2 603 D. 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 #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- CF思维联系--CodeForces - 218C E - Ice Skating (并查集)
题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...
- CodeForces 698B Fix a Tree (并查集应用)
当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...
- Codeforces 977E:Cyclic Components(并查集)
题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...
- codeforces #541 D. Gourmet choice(拓扑+并查集)
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...
随机推荐
- Python之四:控制流
1.If 逻辑判断: if a: b elif c: d else: e 先判断a语句块的值是否为真,如果为真,则执行b语句块,如果不为真则转到elif判断c语句块的值是否为真,如果为真执行d语句块, ...
- Python之三:运算符与表达式
1.运算符: 1.1.运算符种类: 运算符 名称 说明 例子 + 加 5+4 - 减 * 乘 / 除 // 取整除 商的整数部分 3//2,结果 ...
- 0005 uwsgi配置
在配置文件目录Configurations下创建一个名为uwsgi.ini的文件,用于uwsgi服务配置. uwsgi在服务器上使用,接收nginx的转发请求. 内容如下: # 配置文件:这一行必须有 ...
- java面试记录三:hashmap、hashtable、concurrentHashmap、ArrayList、linkedList、linkedHashmap、Object类的12个成员方法、消息队列MQ的种类
口述题 1.HashMap的原理?(数组+单向链表.put.get.size方法) 非线程安全:(1)hash冲突:多线程某一时刻同时操作hashmap并执行put操作时,可能会产两个key的hash ...
- 【你不知道的javaScript 上卷 笔记1】 javaScript 是如何工作的?
一.什么是作用域? 作用域是用来存储变量以及方便寻找变量的一套规则. 二.javaScript 编译过程(编译发生在代码执行前的几微妙) 分词/词法分析(Tokenizing/Lexing)-> ...
- java编码解码过程
最近做项目的时候,有时会遇到中文乱码的问题,网上查询了很多资料,发现大多都是只讲解决方案,并没有讲到为什么要使用这种方案,这种方案的原理是什么? 最典型的就是连接数据库的URL,我们一般把它放到cla ...
- nginx配置从远程获取静态资源
前置条件:现有两台内网互通机器192.168.0.100.192.168.0.101,其中192.168.0.100可以通过外网网络.业务需求:需要通过外网访问处于192.168.0.101机器上的静 ...
- win10子系统ubuntu内的nginx启动问题
需用sudo启动,密码是windows密码.
- 从ASCII到Unicode再到UTF-8的历史原由
编码 大小 支持语言 ASCII 1个字节 英文 Unicode 2个字节(生僻字4个) 所有语言 UTF-8 1-6个字节,英文字母1个字节,汉字3个字节,生僻字4-6个字节 所有语言 具体解释: ...
- url 获取 geoserver 中对应的style
http://userName:password@127.0.0.1:7093/geoserver/rest/workspaces/FAST/styles/AVG_RSRP.sld