题目链接: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. Pikachu-File Inclusion(文件包含漏洞)

    File Inclusion(文件包含漏洞)概述 文件包含,是一个功能.在各种开发语言中都提供了内置的文件包含函数,其可以使开发人员在一个代码文件中直接包含(引入)另外一个代码文件. 比如 在PHP中 ...

  2. php 文件追加写入

    //追加写入 file_put_contents('xml.text',json_encode($postObj,JSON_FORCE_OBJECT).PHP_EOL,FILE_APPEND);

  3. 树莓派中安装ubuntu及相关设置

    一.下载并烧录系统 首先准备好我们要烧录的ubuntu_meta系统,可以在树莓派官网中下载https://www.raspberrypi.org/downloads/ 这里我们选择 Raspberr ...

  4. Codeforce 567A - Lineland Mail

    All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its ...

  5. 配置hosts快速访问GitHub

    经常要clone github中的一些项目,无奈如果不爬梯子的话速度实在是龟速,经常1k/s,于是搜了下解决方法,改HOSTS大法.Windows下在C:/Windows/system32/drive ...

  6. python threading2种调用方式实例

    1.认识GIL: 说到GIL一直是代码专家们一直以来想要解决的问题,也是被许多程序员诟病的,下面带领大家看下官方threading模块document中如何去描述对于GIL这个全局解释器锁的:http ...

  7. cookie和会话

    一.为什么要使用cookie和会话 HTTP是一种无状态技术,这意味着每个单独的HTNML页面都是一个无关的.当人们穿过站点时,HTTP 没有用于跟踪用户或保持变量的方法,尽管浏览器会跟踪你访问过的页 ...

  8. 详解C/C++中的的:#pragma pack(push) 、#pragma pack(pop) 和#pragma pack()

    前言 我们知道结构体内存对齐字节可以通过#pragma pack(n) 的方式来指定. 但是,有没有想过一个问题,某些时候我想4字节对齐,有些时候我又想1字节或者8字节对齐,那么怎么解决这个问题呢? ...

  9. Codeforces Round #601 (Div. 2) E1 Send Boxes to Alice (Easy Version)

    #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int a[N]; int n; bool pr ...

  10. 小匠第二周期打卡笔记-Task05

    一.卷积神经网络基础 知识点记录: 神经网络的基础概念主要是:卷积层.池化层,并解释填充.步幅.输入通道和输出通道之含义. 二维卷积层: 常用于处理图像数据,将输入和卷积核做互相关运算,并加上一个标量 ...