Secret Passwords CodeForces - 1263D(并查集)

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相等的条件是:
a和b存在字符相等
a和b都等于一个字符串b
求有多少个不同字符串

思路:
既然存在相同字符就是相同,那本质就是求独立的字符个数了。
对于一个字符串,字符之间成为了一个连通块,最后看所有字符有几个独立的就好了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>

using namespace std;

const int maxn = 200005;
vector<int>ans;
string a[maxn];
int fa[maxn],vis[maxn],vis2[maxn];

int findset(int x)
{
    if(fa[x] == x)return x;
    return fa[x] = findset(fa[x]);
}

bool Union(int x,int y)
{
    int rx = findset(x),ry = findset(y);
    if(rx != ry)
    {
        fa[rx] = ry;
        return true;
    }
    return false;
}

int main()
{
    int n;scanf("%d",&n);
    for(int i = 1;i <= 200;i++)fa[i] = i;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
    }
    for(int i = 1;i <= n;i++)
    {
        int len = (int)a[i].size();
        int flag = 1;
        for(int j = 0;j < len;j++)
        {
            for(int k = j;k < len;k++)
            {
                int t1 = a[i][j] - 'a' + 1;
                int t2 = a[i][k] - 'a' + 1;
                Union(t1,t2);
                if(!vis[t1])
                {
                    ans.push_back(t1);
                    vis[t1] = 1;
                }
                if(!vis[t2])
                {
                    ans.push_back(t2);
                    vis[t2] = 1;
                }
            }
        }
    }
    
    
    int res = 0, len = (int)ans.size();
    for(int i = 0;i < len;i++)
    {
        int x = findset(ans[i]);
        if(!vis2[x])
        {
            res++;
            vis2[x] = 1;
        }
    }
    
    printf("%d\n",res);
    return 0;
}
发布了676 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104124913