日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Secret Passwords CodeForces - 1263D(并查集)

發布時間:2023/12/15 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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個字符串中的字母,出現在同一字母中的利用并查集更新為一個集合。最后查找出現的這幾個字母中,有幾個祖先就可以了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=30; int f[maxx]; string s; int n,len;inline void init() {for(int i=0;i<=26;i++) f[i]=i; } inline int getf(int u) {return u==f[u]?u:f[u]=getf(f[u]); } inline void merge(int u,int v) {int t1=getf(u);int t2=getf(v);if(t1!=t2) f[t1]=t2; } int main() {scanf("%d",&n);init();set<int> se;for(int i=1;i<=n;i++){cin>>s;len=s.length();for(int j=1;j<len;j++) merge(s[j]-'a',s[j-1]-'a');for(int j=0;j<len;j++) se.insert(s[j]-'a');}map<int,bool> mp;int ans=0;for(set<int> ::iterator it=se.begin();it!=se.end();it++){int zz=getf(*it);if(!mp[zz]) mp[zz]=1,ans++;}cout<<ans<<endl;return 0; }

努力加油a啊,(o)/~

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Secret Passwords CodeForces - 1263D(并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。