Codeforces 1263D. Secret Passwords (disjoint-set, thinking)

Portal

Meaning of the questions:

N to a string, if any two strings with the same character, the same that is, they belong to the same collection, asked a total of several collections

Ideas:

Disjoint-set for each string, each character with the first character of the merger, because they are definitely in the same collection (meaning that each string as a ch [0], look at a few different), after the merger, for (1-26) in which the number of root, i.e., the number of the sets, looks like a thinking title (disjoint-set title bare)

Code:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <math.h>
#include <map>
#include <queue>
#include <set>
using namespace std;
int pre[100];
int vis[100050];
int find(int x)
{
    if(x==pre[x])return x;
    else return pre[x]=find(pre[x]);
}
void join(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx>fy)swap(fx,fy);
    if(fx!=fy)pre[fx]=fy;
}
int main ()
{
    //freopen("in.txt","r",stdin);
    int n;
    char ch[100050];
    for(int i=1; i<=26; ++i)pre[i]=i;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",ch);
        int len=strlen(ch);
        int t=find(ch[0]-'a'+1);
        vis[t]=1;
        for(int i=1;i<len;i++)
        {
            int tt=ch[i]-'a'+1;
            view [TT] = 1 ;
            the Join (T, TT); // with ch [0] merge 
        }
    }
    int ANS = 0 ;
     for ( int I = . 1 ; I <= 26 is ; I ++ )
         IF (pre [I] == I && VIS [I] == . 1 ) // determined on the premise that the character appeared 
            ANS ++ ;
        printf("%d\n",ans);
    //printf("hello, world\n");
}

 

 

Guess you like

Origin www.cnblogs.com/zzl-dreamfly/p/11982195.html