zoukankan      html  css  js  c++  java
  • Play on Words HDU

     Play on Words HDU - 1116 

    Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us. 

    There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door. 

    InputThe input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list. 
    OutputYour program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times. 
    If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.". 
    Sample Input

    3
    2
    acm
    ibm
    3
    acm
    malform
    mouse
    2
    ok
    ok

    Sample Output

    The door cannot be opened.
    Ordering is possible.
    The door cannot be opened.


    题意:给你一些英文字母,不同的英文字母之间可以首位连接,只要字母一样,问可不可以构成一个完整的一条链
    题解:一开始想把每一个单词都看作是一个点来跑,但是很难实现(有点像哈密顿图),之后我就把英文的26个字母当作是点,然后按照单词来建边,当作欧拉通路来跑。
       有向的欧拉通路:只有两个节点的出入度不一样(一个出度比入度大一,一个入度比出度大一),其余所有的点的出入度都是一样的。同时还需要判断是不是一个连通图,那就是看是不是在同一个集合(根节点是不是同一个就可以了)
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<stack>
    #include<map>
    #include<cstdlib>
    #include<vector>
    #include<string>
    #include<queue>
    using namespace std;
    
    #define ll long long
    #define llu unsigned long long
    #define INF 0x3f3f3f3f
    const double PI = acos(-1.0);
    const int maxn =  1e3+10;
    const int mod = 1e9+7;
    int par[30];
    void init()
    {
        for(int i=0;i<30;i++)
            par[i] = i;
    }
    int find(int x)
    {
        return par[x]!=x ? find(par[x]) : x;
    }
    void combine(int a,int b)
    {
        a = find(a);
        b = find(b);
        if(a != b)
            par[a] = b;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int st[30],en[30];
            memset(st,0,sizeof st);
            memset(en,0,sizeof en);
            int n;
            scanf("%d",&n);
            init();
            for(int i=0;i<n;i++)
            {
                char str[1010];
                scanf("%s",str);
                int a=str[0]-'a';
                int b=str[strlen(str)-1]-'a';
                st[a]++;
                en[b]++;
                combine(a,b);
            }
            int start;
            for(int i=0;i<26;i++)
            {
                if((st[i] || en[i]) && i == find(i))
                {
                    //cout<<i<<endl;
                    start = i;
                    break;
                }
            }
            int ans = 0;
            bool connect = 1;
            for(int i=0;i<26;i++)
            {
                ans += abs(st[i]-en[i]);
                if((st[i] || en[i]) && start != find(i))
                    connect = 0;
            }
            if(ans > 2 || connect == 0)
                puts("The door cannot be opened.");
            else
                puts("Ordering is possible.");
        }
    }
    View Code
  • 相关阅读:
    找不到 .NETFramework,Version=v5.0 的引用程序集。要解决此问题,请为此框架版本安装开发人员工具包(SDK/目标包)或者重新定向应用程序。
    从pfx私钥证书中提取私钥
    更改域密码
    realtek高清晰音频管理器 WIN10
    LINQ to Entities does not recognize the method 'System.String ToString()' method
    C# ML.NET 使用GPU遇到 Failed to get convolution algorithm.This is probably because cuDNN failed to initialize
    8款开源自动化测试框架
    质量管理大师:戴明、克劳士比、朱兰、菲根堡姆
    接口测试数据建模
    稻盛和夫:一场高效的会议,一定是让员工多说
  • 原文地址:https://www.cnblogs.com/smallhester/p/10331292.html
Copyright © 2011-2022 走看看