[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 n 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 a a and b b b as follows:

two passwords a a a and b b b are equivalent if there is a letter, that exists in both a a a and b b b;
two passwords a a a and b b b are equivalent if there is a password c c c from the list, which is equivalent to both a a a and b b 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.

输入

The first line contain integer n ( 1 ≤ n ≤ 2 × 1 0 5 ) n (1≤n≤2\times 10^5) n(1n2×105) — number of passwords in the list. Next n lines contains passwords from the list – non-empty strings s i s_i si, with length at most 50 50 50letters. Some of the passwords may be equal.

It is guaranteed that the total length of all passwords does not exceed 1 0 6 10^6 106 letters. All of them consist only of lowercase Latin letters.

输出

In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.

题目大意

给定 n n n个单词,单词全由小写字母构成,保证每个单词不超过 50 50 50个字母,总长度不超过 1 0 6 10^6 106个字母。
两个单词是等效的,当且仅当:

  1. 两个单词中有任一字母相等。
  2. 存在一中间单词,使得两个单词都与中间单词相等。

即:
a = c , b = c a = c,b = c a=c,b=c,则有 a = b a=b a=b

思路

显然等效的单词是一个集合,容易想到用并查集维护,那么最后答案就是集合个数。
但如何维护就成了问题。
发现其实所有操作都在考虑字母,因此可以直接维护字母的并查集,而每个单词就是一个集合,因此每次读入单词时将单词内所有字母合并到一个集合中。
但还需要考虑没有出现的字母,遍历单词时记录一下即可。
复杂度 O ( n ) , n ≤ 1 0 6 O(n),n≤10^6 O(n),n106

代码

#include <cstdio>
#include <cstring>
using namespace std;
int fa[27], vis[27];
inline int find(const int &x)
{
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}
inline void unite(const int &a, const int &b)
{
    int t1 = find(a), t2 = find(b);
    if (t1 == t2)
        return;
    fa[t1] = t2;
}
char all[60];
int n, len,ans;
inline char nc()
{
    static char buf[1050000],*p1 = buf,*p2 = buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1050000,stdin),p1==p2)?EOF:*p1++;
}
void read(char *s)
{
    static char c;
    for(c=nc();c>'z'||c<'a';c=nc());
    for(;c>='a'&&c<='z';*(++s) = c,c=nc());
    *(++s) = '\0';//快读的细节,为了防止上次读入内容影响到,要加文本终止符
}
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= 26; ++i)
        fa[i] = i;//初始化
    for (int i = 1; i <= n; ++i)
    {
        read(all);
        len = strlen(all + 1);
        for (int j = 1; j <= len; ++j)
            unite(all[j] - 'a' + 1, all[1] - 'a' + 1), vis[all[j] - 'a' + 1] = 1;
    }
    for(int i = 1;i<=26;++i)
        if(vis[i] && find(i) == i)
            ++ans;
    printf("%d",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值