Codeforces1263D-Secret Passwords
题意
给n个字符串,两个字符串之间如果有相同的字符,那么两个就等价,等价关系可以传递,问最后有多少个等价类。
分析
- 考虑并查集或者dfs联通块,如果是并查集的话,对于当前字符串的某个字符,肯定要和这个字符在前面出现的某个字符串合并起来,所以维护每个字符的最后一次出现位置即可。
- 如果是dfs找联通块,建图就是把26个字符看成26个点,然后字符串分别连向这些字符,最后从字符节点dfs标记。
代码
- dfs
#include <bits/stdc++.h>
using namespace std;
const int N=2e6+50;
int n;
char s[55];
struct Edge{
int v,next;
}e[N*2];
int cnt,head[N];
int ind[N];
void init(){
cnt=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v){
e[cnt]=Edge{v,head[u]};
head[u]=cnt++;
e[cnt]=Edge{u,head[v]};
head[v]=cnt++;
}
int v[30],vis[N];
void dfs(int u){
vis[u]=1;
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v;
if(vis[v]){
continue;
}
dfs(v);
}
}
int main(){
scanf("%d",&n);
init();
for(int i=1;i<=n;i++){
scanf("%s",s+1);
int len=strlen(s+1);
memset(v,0,sizeof(v));
for(int j=1;j<=len;j++){
if(!v[s[j]-'a'+1]){
ind[s[j]-'a'+1]++;
add(26+i,s[j]-'a'+1);
v[s[j]-'a'+1]=1;
}
}
}
int ans=0;
for(int i=1;i<=26;i++){
if(ind[i] && !vis[i]){
dfs(i);
ans++;
}
}
printf("%d\n",ans);
return 0;
}
- 并查集
#include <bits/stdc++.h>
using namespace std;
const int N=2e6+50;
int p[N];
int n;
char s[55];
int let[30];
void init(int n){
for(int i=1;i<=n;i++){
p[i]=i;
}
}
int find(int x){
return x==p[x]?p[x]:p[x]=find(p[x]);
}
int main(){
scanf("%d",&n);
init(n);
for(int i=1;i<=n;i++){
scanf("%s",s+1);
int len=strlen(s+1);
for(int j=1;j<=len;j++){
if(let[s[j]-'a'] && let[s[j]-'a']!=i){
int fa=find(let[s[j]-'a']);
int fb=find(i);
if(fa!=fb){
p[fa]=fb;
}
}
let[s[j]-'a']=max(let[s[j]-'a'],i);
}
}
int ans=0;
for(int i=1;i<=n;i++){
if(p[i]==i){
ans++;
}
}
printf("%d\n",ans);
return 0;
}
Codeforces1263D-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]1263D Secret Passwords
题目 One unknown hacker wants to get the admin's password of AtForces testing system, to get problems ...
- codeforces div2 603 D. Secret Passwords(并查集)
题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...
- cisco 密码重置
密码重置 分类: 转贴技术资料 2007-12-28 16:38 http://www.cisco.com/en/US/products/hw/routers/ps259/products_passw ...
- CF#603 Div2
差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...
- Codeforces Round #603 (Div. 2)
传送门 感觉脑子还是转得太慢了QAQ,一些问题老是想得很慢... A. Sweet Problem 签到. Code /* * Author: heyuhhh * Created Time: 2019 ...
- 3-Consul 使用手册
原文:http://www.liangxiansen.cn/2017/04/06/consul/ Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键 ...
- Codeforces Round #603 (Div. 2) (题解)
A. Sweet Problem (找规律) 题目链接 大致思路: 有一点瞎猜的,首先排一个序, \(a_1>a_2>a_3\) ,发现如果 \(a_1>=a_2+a_3\) ,那么 ...
- (转载) Consul 使用手册(感觉比较全了)
使用consul 介绍 Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键特性: 服务发现 Consul的客户端可用提供一个服务,比如 api 或者 ...
随机推荐
- node.js获取ip及mac
; (function (win) { var os = require('os'); var ifaces = os.networkInterfaces(); function NetworkUti ...
- Consider defining a bean of type `xxx` in your configuration问题解决
在使用SpringBoot装配mybatis时出现了异常 *************************** APPLICATION FAILED TO START *************** ...
- centos6.9实现双网卡绑定
1.创建bond0文件 # vi /etc/sysconfig/network-scripts/ifcfg-bond0 DEVICE=bond0 NM_CONTROLLED=no #是否由networ ...
- Linux设备驱动程序 之 Makefile
典型的模块Makefile如下所示: ifneq ($(KERNELRELEASE),) obj-m := hello.o else KERNELDIR ?=/lib/modules/$(shell ...
- Twisted框架学习
Twisted是用Python实现的基于事件驱动的网络引擎框架,是python中一个强大的异步IO库.理解twisted的一个前提是弄清楚twisted中几个核心的概念: reactor, Proto ...
- 客户端连接虚拟机上的MYSQL报错
这个原因是因为虚拟机的数据库拒绝其他主机访问 所以需要设置虚拟机的mysql 打开mysql mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' ID ...
- LC 781. Rabbits in Forest
In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how ...
- RabbitMQ学习之:(十一)AMQP.0-10规范,中文翻译1,2,3章 (转载)
From:http://blog.sina.com.cn/s/blog_4aba0c8b0100p6ho.html From: http://blog.sina.com.cn/s/blog_4aba0 ...
- 更改mysql 数据库 utf8
mysql> alter database 数据库名 character set utf8;
- DES算法概述
DES全称为Data Encryption Standard,即数据加密标准.1997年数据加密标准DES正式公布,其分组长度为64比特,密钥长度为64比特,其中8比特为奇偶校验位,所以实际长度为56 ...