UVA 10129 Play on Words

22 篇文章 0 订阅
10 篇文章 0 订阅

题目如下:

Play on Words

Some of the secret doorscontain a very interesting word puzzle. The team of archaeologists has tosolve 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 largenumber of magnetic plates on every door. Every plate has one word written onit. The plates must be arranged into a sequence in such a way that every wordbegins with the same letter as the previous word ends. For example, the word``acm'' can be followed by the word ``motorola''. Your task is towrite a computer program that will read the list of words and determine whetherit is possible to arrange all of the plates in a sequence (according tothe given rule) and consequently to open the door.

Input Specification

The inputconsists of T test cases. The number of them (T) is givenon the first line of the input file. Each test case begins with a linecontaining a single integer number Nthat indicates the numberof plates (1 <= N <= 100000). Then exactly Nlinesfollow, each containing a single word. Each word contains at least two andat most 1000 lowercase characters, that means only letters 'a'through 'z' will appear in the word. The same word mayappear several times in the list.

Output Specification

Yourprogram has to determine whether it is possible to arrange all the plates ina sequence such that the first letter of each word is equal to the lastletter of the previous word. All the plates from the list must be used, eachexactly once. The words mentioned several times must be used that number oftimes.

If there exists such anordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".

Sample Input

32acmibm3acmmalformmouse2okok

Output for the SampleInput

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


这道题刚开始没发现是欧拉路,直接DFS,果断超时了TAT,然后改成欧拉回路,有没注意到要连通还有欧拉路可以只有一个节点是奇节点,WA了两次TAT,改过来后AC了。

AC的代码如下:

#include 
   
   
    
    

using namespace std;
int T,N;
int in[26],out[26];
int graph[26][26];
int vis[26];
void dfs(int u)
{
    if(vis[u])
        return;
    vis[u]=1;
    for(int i=0; i<26; i++)
        if(graph[u][i]==1&&!vis[i])
            dfs(i);


}
int main()
{
    cin>>T;
    while(T--)
    {
        cin>>N;
        string s;
        memset(vis,1,sizeof vis);
        memset(in,0,sizeof in);
        memset(out,0,sizeof out);
        for(int i=0; i
    
    
     
     >s;
            in[s[s.size()-1]-'a']++;
            out[s[0]-'a']++;
            graph[s[0]-'a'][s[s.size()-1]-'a']=1;
            vis[s[0]-'a']=0;
            vis[s[s.size()-1]-'a']=0;
        }

        int numin=0,numout=0;
        int ok=1;
        for(int i=0; i<26; i++)
        {
            if(!vis[i])
            {
                dfs(i);
                break;
            }

        }
        for(int i=0; i<26; i++)
            if(!vis[i])
            {
                ok=0;
                cout<<"The door cannot be opened."<
     
     
    
    
   
   

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值