D. Secret Passwords

14 篇文章 0 订阅

D. Secret Passwords

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn 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 aa and bb as follows:

  • two passwords aa and bb are equivalent if there is a letter, that exists in both aa and bb;
  • two passwords aa and bb are equivalent if there is a password cc from the list, which is equivalent to both aa and bb.

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 nn (1≤n≤2⋅1051≤n≤2⋅105) — number of passwords in the list. Next nn lines contains passwords from the list – non-empty strings sisi, with length at most 5050 letters. Some of the passwords may be equal.

It is guaranteed that the total length of all passwords does not exceed 106106 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

Copy

4
a
b
ab
d

output

Copy

2

input

Copy

3
ab
bc
abc

output

Copy

1

input

Copy

1
codeforces

output

Copy

1

Note

In the second example hacker need to use any of the passwords to access the system.

题意:我死在题意上了

题意就是每两个个字符串中只要存在相同字符就能合并成一个,问最小能剩下几个

思路:并查集并一下每个字符串的每个字符看看他们最后有几块即可

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+9;
int f[maxn];
map<int,int>book;
int find(int x) {
	if(x == f[x]) {
		return x;
	}
	else {
		return f[x] = find(f[x]);
	}
}
void union1(int x, int y) {
	x = find(x);
	y = find(y);
	f[x] = y;
}		
string s[maxn];
int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1;i <= maxn;++i) {
		f[i] = i;
	} 
	for(int i = 1;i <= n;++i) {
		cin >> s[i];
		for(int j = 1;j < s[i].length();++j) {
			
			union1(s[i][j]-'a',s[i][j-1]-'a');
		}
	}  
	int sum = 0;
	for(int i = 1;i <= n;++i) {
		for(int j = 0;j < s[i].length();++j) {
			if(!book[find(s[i][j]-'a')]) {
				book[find(s[i][j]-'a')] = 1;
				sum++;
			}
		}
	} 
	printf("%d\n", sum);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-lyslyslys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值