Codeforces #603(div.2)D题,并查集

密码等价性破解
探讨了在一个由n个密码组成的列表中,如何确定最小数量的密码以确保能够访问系统的问题。密码之间的等价性基于共享字母,且此等价性具有传递性。通过构建集合并进行合并操作,算法最终计算出所需的最小密码数量。

# 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个密码,密码与密码之间有等价性,如果两个密码都有同一个字母,那么可以说这两个密码是等价的,其次等价性是可以传递的,即如果a等价b,b等价c,即使a与c不存在相同的字母,但他们也可以说是等价的;
你解开这个系统需要所有的密码,具有等价性的密码是可以通用的,那么你至少需要多少个密码?

思路是这样的,先构建这样的n+26个集合,初始时刻,第i个字符串属于第i个集合,然后第i+1个集合对应字母a…第i+26个集合对应字母z;
然后遍历每一个字符串,让每一个字符串与该字符串中所包含的字母合并集合,然后再一次遍历所有的字符串,数出有几个不同的集合

#include <cstdio>
#include <set>
#include <string>
#include <iostream>
using namespace std;
const int N = 2e5 + 10;
string s[N];
int fa[N+26];
int n;
int find(int x){
	return fa[x] == x?x:fa[x] = find(fa[x]);
}
void merge(int x,int y){
	int fx = find(x);
	int fy = find(y);
	if(fx != fy){
		fa[fx] = fy;
	}
}
int main(){
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		cin>>s[i];
	}
	for(int i = 1;i <= n+26;i++) fa[i] = i;
	for(int i = 1;i <= n;i++){
		for(int j = 0;j < s[i].length();j++){
			merge(i,n+s[i][j] - 'a'+1);
		}
	}
	set<int> myset;
	int ans = 0;
	for(int i = 1;i <= n;i++){
		if(!myset.count(find(i))){
			ans++;
			myset.insert(find(i));
		}
	}
	printf("%d\n",ans);
	return 0;
}
Java是一种具备卓越性能与广泛平台适应性的高级程序设计语言,最初由Sun Microsystems(现属Oracle公司)的James Gosling及其团队于1995年正式发布。该语言在设计上追求简洁性、稳定性、可移植性以及并发处理能力,同时具备动态执行特性。其核心特征与显著优点可归纳如下: **平台无关性**:遵循“一次编写,随处运行”的理念,Java编写的程序能够在多种操作系统与硬件环境中执行,无需针对不同平台进行修改。这一特性主要依赖于Java虚拟机(JVM)的实现,JVM作为程序与底层系统之间的中间层,负责解释并执行编译后的字节码。 **面向对象范式**:Java全面贯彻面向对象的设计原则,提供对封装、继承、多态等机制的完整支持。这种设计方式有助于构建结构清晰、模块独立的代码,提升软件的可维护性与扩展性。 **并发编程支持**:语言层面集成了多线程处理能力,允许开发者构建能够同时执行多项任务的应用程序。这一特性尤其适用于需要高并发处理的场景,例如服务器端软件、网络服务及大规模分布式系统。 **自动内存管理**:通过内置的垃圾回收机制,Java运行时环境能够自动识别并释放不再使用的对象所占用的内存空间。这不仅降低了开发者在内存管理方面的工作负担,也有效减少了因手动管理内存可能引发的内存泄漏问。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值