题目

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 nnn 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 aaa and bbb as follows:

two passwords aaa and bbb are equivalent if there is a letter, that exists in both aaa and bbb;

two passwords aaa and bbb are equivalent if there is a password ccc from the list, which is equivalent to both aaa and bbb.

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:

  1. admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab";
  2. 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×105)n (1≤n≤2\times 10^5)n(1≤n≤2×105) — number of passwords in the list. Next n lines contains passwords from the list – non-empty strings sis_isi​, with length at most 505050letters. Some of the passwords may be equal.

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

题目大意

给定nnn个单词,单词全由小写字母构成,保证每个单词不超过505050个字母,总长度不超过10610^6106个字母。

两个单词是等效的,当且仅当:

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

即:

a=c,b=ca = c,b = ca=c,b=c,则有a=ba=ba=b。

思路

显然等效的单词是一个集合,容易想到用并查集维护,那么最后答案就是集合个数。

但如何维护就成了问题。

发现其实所有操作都在考虑字母,因此可以直接维护字母的并查集,而每个单词就是一个集合,因此每次读入单词时将单词内所有字母合并到一个集合中。

但还需要考虑没有出现的字母,遍历单词时记录一下即可。

复杂度O(n),n≤106O(n),n≤10^6O(n),n≤106

代码

  1. #include <cstdio>
  2. #include <cstring>
  3. using namespace std;
  4. int fa[27], vis[27];
  5. inline int find(const int &x)
  6. {
  7. return fa[x] == x ? x : fa[x] = find(fa[x]);
  8. }
  9. inline void unite(const int &a, const int &b)
  10. {
  11. int t1 = find(a), t2 = find(b);
  12. if (t1 == t2)
  13. return;
  14. fa[t1] = t2;
  15. }
  16. char all[60];
  17. int n, len,ans;
  18. inline char nc()
  19. {
  20. static char buf[1050000],*p1 = buf,*p2 = buf;
  21. return p1==p2&&(p2=(p1=buf)+fread(buf,1,1050000,stdin),p1==p2)?EOF:*p1++;
  22. }
  23. void read(char *s)
  24. {
  25. static char c;
  26. for(c=nc();c>'z'||c<'a';c=nc());
  27. for(;c>='a'&&c<='z';*(++s) = c,c=nc());
  28. *(++s) = '\0';//快读的细节,为了防止上次读入内容影响到,要加文本终止符
  29. }
  30. int main()
  31. {
  32. scanf("%d", &n);
  33. for (int i = 1; i <= 26; ++i)
  34. fa[i] = i;//初始化
  35. for (int i = 1; i <= n; ++i)
  36. {
  37. read(all);
  38. len = strlen(all + 1);
  39. for (int j = 1; j <= len; ++j)
  40. unite(all[j] - 'a' + 1, all[1] - 'a' + 1), vis[all[j] - 'a' + 1] = 1;
  41. }
  42. for(int i = 1;i<=26;++i)
  43. if(vis[i] && find(i) == i)
  44. ++ans;
  45. printf("%d",ans);
  46. return 0;
  47. }

[Codeforces]1263D Secret Passwords的更多相关文章

  1. Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集

    D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...

  2. Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)

    链接: https://codeforces.com/contest/1263/problem/D 题意: One unknown hacker wants to get the admin's pa ...

  3. codeforces div2 603 D. Secret Passwords(并查集)

    题目链接:https://codeforces.com/contest/1263/problem/D 题意:有n个小写字符串代表n个密码,加入存在两个密码有共同的字母,那么说这两个密码可以认为是同一个 ...

  4. CodeForces 496B Secret Combination

    Secret Combination Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  5. codeforces 496B. Secret Combination 解题报告

    题目链接:http://codeforces.com/problemset/problem/496/B 题目意思:给出 n 位数你,有两种操作:1.将每一位数字加一(当某一位 > 9 时只保存个 ...

  6. codeforces 721B B. Passwords(贪心)

    题目链接: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. 【37.21%】【codeforces 721B】Passwords

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. [Codeforces] #603 (Div. 2) A-E题解

    [Codeforces]1263A Sweet Problem [Codeforces]1263B PIN Code [Codeforces]1263C Everyone is a Winner! [ ...

  9. Codeforces Round #603 (Div. 2)

    传送门 感觉脑子还是转得太慢了QAQ,一些问题老是想得很慢... A. Sweet Problem 签到. Code /* * Author: heyuhhh * Created Time: 2019 ...

随机推荐

  1. Java基础知识笔记第六章:接口

    接口 /* 使用关键字interface来定义一个接口.接口的定义和类的定义很相似,分为接口声明和接口体 */ interface Printable{ final int max=100; void ...

  2. Codeforces Round #579 (Div. 3)D(字符串,思维)

    #include<bits/stdc++.h>using namespace std;char s[200007],t[200007];int last[200007][27],nxt[2 ...

  3. Android之Builder对话框的一些常用方式

    原文: http://blog.csdn.net/kkfdsa132/article/details/6322835 Android为我们提供几种对话框,主要有:AlertDialog.Progres ...

  4. [swscaler @ ...] deprecated pixel format used, make sure you did set range correctly

    我自己在使用如下函数进行转换时报的错 int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[], const int sr ...

  5. JS垂直落体回弹原理

    /* *JS垂直落体回弹原理 */ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  6. Windows xp Diskpart合并分区的方法

    非常不错的合并分区的方法,经测试,好用,就是对于稳定性就不知道了,理论下应该没什么问题,对于个人电脑合并分区和服务器分区合并来说,无疑是一个非常好的办法     分区增容就是当一个分区的空间不能满足使 ...

  7. smoj2828子数组有主元素

    题面 一个数组B,如果有其中一个元素出现的次数大于length(B) div 2,那么该元素就是数组B的主元素,显然数组B最多只有1个主元素,因为数组B有主元素,所以被称为"优美的" ...

  8. shell脚本中 “set -e” 的作用

    #!/bin/bash set -e command 1command 2 每个脚本都应该在文件开头加上set -e,这句语句告诉bash如果任何语句的执行结果不是true则应该退出.这样的好处是防止 ...

  9. 二 SSH整合:Spring整合Hibernate,无障碍整合&无核心配置整合,Hibernate模版常用方法,

    重建SSH项目 java项目可以直接复制,但是web项目除了改名字还要该配置,如下: 方式一:无障碍整合:带Hibernate配置文件 <?xml version="1.0" ...

  10. axios发送post请求node服务器无法通过req.body获取参数

    问题: 项目前端使用Vue框架,后端使用node.js搭建本地服务器.前端通过 axios 方式请求后端数据的过程中,发现如果是 get 请求,服务器端能够通过 req.query 获取前端传递的参数 ...