BNU18547 UVA10129 Play on Words

Play on Words

Time Limit: 3000ms
Memory Limit: 131072KB
This problem will be judged on UVA. Original ID: 10129
64-bit integer IO format: %lld      Java class name: Main
Type: 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •                    
  • [PDF Link]

    Play on Words

    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.

    Input Specification

    The 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.

    Output Specification

    Your 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

    Output for the Sample Input

    The door cannot be opened.
    Ordering is possible.
    The door cannot be opened.
    https://www.bnuoj.com/v3/problem_show.php?pid=18547
    给几个单词 看他们能不能排成一列要求前一个单词的最后一个字母 跟后一个单词首字母相同
    欧拉图 把字母当成节点 单词当做边

    #include<bits/stdc++.h>
    using namespace std;
    int vis[100][100],point[100],jj[100][100];
    char str[1010];
    vector<int>ap[100];
    int cnt,flag,n;
    void dfs(int u)
    {
        for(int i=0; i<ap[u].size(); i++)
        {
            int v=ap[u][i];
            if(vis[u][v]<jj[u][v])//多个相同单词
            {
                vis[u][v]++;
                cnt++;
                dfs(v);
                if(flag) return ;
            }
        }
        if(cnt==n)
        flag=1;
    }
    
    int main()
    {
        int t,h,u,sum,i;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&n);
            for(i=0; i<100; i++)
                ap[i].clear();
            //memset(ap,0,sizeof(ap));
            memset(point,0,sizeof(point));
            memset(jj,0,sizeof(jj));
            for(h=1; h<=n; h++)
            {
                scanf("%s",str);
                int len=strlen(str);
                int a=str[0]-97;
                int b=str[len-1]-97;
                //ap[a][b]=1;
                ap[a].push_back(b);
                jj[a][b]++;   //有向图
                point[a]--;
                point[b]++;
            }
            sum=0;
            for(i=0; i<26; i++)
                if(point[i]>1||point[i]<-1)
                    sum++;
    
            if(sum>2)
            {
                printf("The door cannot be opened.\n");
                continue;
            }
    
            for(i=0; i<26; i++)
                if(sum==0)
                {
                    cnt=0;
                    memset(vis,0,sizeof(vis));
                    flag=0;
                    dfs(i);
                    break;
                }
                else
                {
                    if(point[i]==1)
                    {
                        cnt=0;
                        memset(vis,0,sizeof(vis));
                        flag=0;
                        dfs(i);
                    }
                }
            if(cnt==n)
                printf("Ordering is possible.\n");
            else printf("The door cannot be opened.\n");
        }
        return 0;
    }
    

    刚开始在UVA上写了错的也A了 有点可怕的


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

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值