Codeforces 1263D Secret Passwords (并查集)

Description:

\quad 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.

\quad 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 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 and b are equivalent if there is a password c from
    the list, which is equivalent to both a a a and b b b.

\quad 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.

\quad For example, if the list contain passwords " a " , " b " , " a b " , " d " , "a", "b", "ab", "d", "a","b","ab","d", then passwords " a " , " b " , " a b " "a", "b", "ab" "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 " "b" "b", then you can access to system by using any
    of this passwords: " a " , " b " , " a b " ; "a", "b", "ab"; "a","b","ab";
  • admin’s password is " d " "d" "d" then you can access to system by using
    only " d " . "d". "d".

\quad 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

\quad The first line contain integer n ( 1 ≤ n ≤ 2 ∗ 1 0 5 ) n\left(1≤n≤2*10^{5} \right) n(1n2105)— 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.

\quad 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

\quad 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

\quad In the second example hacker need to use any of the passwords to access the system.
\quad 题意挺简单的就是给你几个串如果a串和c串有相同字母,而且b串也和c有相同字母,那么a,b也是一类的,让求一共有几类。
并查集模板题

ACD代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret*10 + ch - '0';
        ch = getchar();
    }
    return ret*sgn;
}
inline void Out(int a)    //Êä³öÍâ¹Ò
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}

int qpow(int m, int k, int mod)
{
    int res = 1, t = m;
    while (k)
    {
        if (k&1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}

// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
    return qpow(a,p-2,p);
}

ll gcd(ll a,ll b)
{
    return b==0?a : gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*b/gcd(a,b);
}

int exgcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int g=exgcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-a/b*y;
    return g;
}

int mod_reverse(int a,int p)//a*x=1(mod p) 求a的逆元x
{
    int d,x,y;
    d=exgcd(a,p,x,y);
    if(d==1)
        return (x%p+p)%p;
    else
        return -1;
}

int n,ans=0;
int i,j,k;
int t;
int res,temp,cnt,num;
const int maxn = 30;
bool flag[maxn],ok[maxn];
int pre[maxn];
string s;


int Find(int x)
{
    int r=x;
    while(r!=pre[r])
        r=pre[r];
    int i=x,j;
    while(pre[i]!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }//路径压缩
    return r;
}

void mix(int x,int y)
{
	int fx=Find(x),fy=Find(y);
	if(fx!=fy)
	{
		pre[fy]=fx;
	}
}

int main()
{
    sd(n);
    rep(i,1,26)
    pre[i]=i;
    while(n--)
    {
        cin>>s;
        int t=s[0]-'a'+1;
        int len=s.size();
        flag[t]=1;
        res=Find(t);
        rep(i,1,len-1)
        {
            t=s[i]-'a'+1;
            mix(t,res);
            flag[t]=1;
        }
    }
    rep(i,1,26)
    {
        pre[i]=Find(pre[i]);
        if(flag[i]&&!ok[pre[i]])
        {
            ans++;
            ok[pre[i]]=1;
        }
    }
    pd(ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值