• POJ1386 Play on Words


    题意:判断一些单词能不能首尾连成一体

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    #include <cstdio>
    using namespace std;
    int n,father[30],range[30],save[100010],in[30],out[30];
    bool use[30];
    char s[1010];
    void add(int x)
    {
        use[x]=true;
        father[x]=x;
        range[x]=0;
    }
    int Find(int x)
    {
        int k=0;
        while(x!=father[x])
        {
            save[k++]=x;
            x=father[x];
        }
        for(int j=0; j<k; j++)
            father[save[j]]=x;
        return x;
    }
    void uni(int x,int y)
    {
        int a=Find(x),b=Find(y);
        if(a!=b)
        {
            if(range[a]<range[b]) father[a]=b;
            else
            {
                father[b]=a;
                if(range[a]==range[b]) range[a]++;
            }
        }
    }
    int main()
    {
        freopen("in.txt","r",stdin);
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            memset(use,false,sizeof(use));
            memset(father,0,sizeof(father));
            memset(range,0,sizeof(range));
            memset(in,0,sizeof(in));
            memset(out,0,sizeof(out));
            for(int i=1; i<=n; i++)
            {
                scanf("%s",s);
                int l=strlen(s),x=s[0]-'a',y=s[l-1]-'a';
                if(use[x]==false) add(x);
                if(use[y]==false) add(y);
                in[x]++;
                out[y]++;
                uni(x,y);
            }
            int flag=0;
            for(int i=0; i<26; i++)
            {
                if(use[i]==true&&Find(i)==i)
                {
                    flag++;
                }
            }
            if(flag>1) printf("The door cannot be opened.
    ");
            else
            {
                int a=0,b=0,i;
                for(i=0;i<26;i++)
                {
                    if(use[i]&&in[i]!=out[i])
                    {
                        if(in[i]-out[i]==1) a++;
                        else if(out[i]-in[i]==1) b++;
                        else break;
                    }
                }
                if(i<26) printf("The door cannot be opened.
    ");
                else if(a+b==0||a==1&&b==1) printf("Ordering is possible.
    ");
                else printf("The door cannot be opened.
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    web中的安全编码
    网站安全(学习)
    head 命令(转)
    less 命令(转)
    简单网站优化
    Yahoo团队总结的关于网站性能优化的经验(转)
    more命令(转)
    linux安装oracle
    Ubuntu系统环境变量配置文件(转)
    nl命令(转)
  • 原文地址:https://www.cnblogs.com/d-e-v-i-l/p/4783005.html
Copyright © 2020-2023  润新知