Codeforces 1263D(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.

题意:
给定你n个字符串,只要任意两个字符串中有一个相同字符,则这两个字符串属于同一个集合,试问总共有多少个集合。
思路:
把每个字符串看成一个集合,运用并查集,将它们与第一个字母合并,合并后的结果是可以将这个字符串看成一个字符。最终遍历找出答案即可。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 15;
int pre[30], vis[maxn];
void init() {
	for (int i = 0; i < 26; i++)
		pre[i] = i;
}
int Find(int x) {
	if (x == pre[x])
		return x;
	else
		return pre[x] = Find(pre[x]);
}
void unite(int x, int y) {
	int xx = Find(x);
	int yy = Find(y);
	if (yy > xx)
		swap(xx, yy);
	if (xx != yy)
		pre[xx] = yy;
	return;
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	init();
	int n; cin >> n;
	for (int i = 0; i < n; i++) {
		string str;
		cin >> str;
		int x = Find(str[0] - 'a');
		vis[x] = 1;
		for (int j = 1; j < str.length(); j++) {
			int y = str[j] - 'a';
			vis[y] = 1;
			unite(x, y);
		}
	}
	int cnt = 0;
	for (int i = 0; i < 26; i++) {
		if (pre[i] == i && vis[i] == 1)
			cnt++;
	}
	cout << cnt << endl;
	return 0;
}
发布了40 篇原创文章 · 获赞 6 · 访问量 1412

猜你喜欢

转载自blog.csdn.net/qq_43321732/article/details/103973256