1236D. Secret Passwords

一、Problem

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.

二、Example

input

4
a
b
ab
d

output

2

input

3
ab
bc
abc

output

3

input

1
codeforces

output

1

三、Code

package com.codeforces.contests;

import java.io.PrintWriter;
import java.util.Scanner;
 
public class SecretPasswords {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int wheel = in.nextInt();
		String temp;
		boolean[] total = new boolean[26], flag;
		DSU obj = new DSU();
		
		while(wheel-- > 0) {
			temp = in.next();
			flag = new boolean[26];
			
			for (char item : temp.toCharArray()) {
				flag[item - 'a'] = true; 
			}
			for (int i = 0; i < 26; i++) {
				if (flag[i]) {
					total[i] = true;
					obj.union(temp.charAt(0) - 'a', i);
				}
			}
		}
		
		int count = 0;
		for (int i = 0; i < 26; i++) {
			if (total[i] && obj.getParent(i) == i) {
				count += 1;
			}
		}
		out.println(count);
		out.flush();
	}
}
 
class DSU {
	int[] p;
	DSU() {
		p = new int[26];
		for (int i = 0; i < 26; i++) {
			p[i] = i;
		}
	}
	
	int getParent(int i) {
		if (p[i] == i) {
			return i;
		}
		return p[i] = getParent(p[i]);
	}
	
	void union(int i, int j) {
		i = getParent(i);
		j = getParent(j);
		if (i != j) {
			p[j] = getParent(i);
		}
	}
}
发布了332 篇原创文章 · 获赞 198 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/riemann_/article/details/103355829