题目链接: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(并查集)的更多相关文章

  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 #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  3. Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分

    D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...

  4. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  5. CodeForces Roads not only in Berland(并查集)

    H - Roads not only in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  6. CF思维联系--CodeForces - 218C E - Ice Skating (并查集)

    题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...

  7. CodeForces 698B Fix a Tree (并查集应用)

    当时也是想到了并查集,但是有几个地方没有想清楚,所以就不知道怎么写了,比如说如何确定最优的问题.赛后看了一下别人的思路,才知道自己确实经验不足,思维也没跟上. 其实没有那么复杂,这个题目我们的操作只有 ...

  8. Codeforces 977E:Cyclic Components(并查集)

    题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...

  9. codeforces #541 D. Gourmet choice(拓扑+并查集)

    Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...

随机推荐

  1. chm文件打开空白无内容

    问题描述 chm文件打开空白无内容 解决方案 选中 chm 文件,右键 - 属性- 在弹出的对话框中点击右下方的“接触锁定”,之后点击“应用”,再点击“确定”

  2. std::ref和std::cref使用(转载)

    转载于:https://blog.csdn.net/lmb1612977696/article/details/81543802 std::ref和std::cref 解释: std::ref 用于包 ...

  3. 【大道至简】NetCore3.1快速开发框架一:集成Swagger

    在上一章节中,我们创建了基本的框架结构:https://www.cnblogs.com/fuyu-blog/p/12217647.html 下面我们测试接口和集成Swagger接口文档 一.接口测试 ...

  4. 使用nohup不产生log文件方法

    思想 无法阻止nohup产生日志可以将其定向到空文件实现 实现 $ nohup xxx >/dev/null 2>&1 &

  5. Struts2-057远程代码执行漏洞(s2-057/CVE-2018-11776)复现

    参考了大佬的链接:https://github.com/jas502n/St2-057 00x01前言 Apache Struts是美国阿帕奇(Apache)软件基金会负责维护的一个开源项目,是一套用 ...

  6. [JSOI2013] 快乐的 JYY - 回文自动机,DFS

    #include <bits/stdc++.h> #define Sigma 30 #define MAXN 500010 #define int long long using name ...

  7. ReviewBoard使用:添加SVN

    1.登录ReviewBoard,选择“Admin” 2.选择 “Repositores”,点击按钮“Add” 3.填写内容,然后点击按钮“SAVE”保存 Name:仓库名称(自己随意写) Hostin ...

  8. java序列化与反序列化的使用

    个人博客 地址:http://www.wenhaofan.com/article/20180925214701 1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字 ...

  9. Node.js、npm和webpack的安装

    1. 前往Node.js官网下载安装程序 2. 一路点击下一步即可 3. 测试是否安装成功 4. 配置npm在安装全局模块时的路径和缓存cache的路径 因为在执行例如npm install webp ...

  10. PyQt5+Eric6开发的一个使用菜单栏、工具栏和状态栏的示例

    前言 在做一个数据分析的桌面端程序遇到一些问题,这里简单整理下,分享出来供使用者参考. 1.网上查使用PyQt5工具栏的示例,发现很多只是一个简单的退出功能,如果有几个按钮如何处理?如何区分点击的究竟 ...