HDU-1116-Play on Words

Play on Words

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3818 Accepted Submission(s): 1229


Problem Description
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
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
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


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



题意:给你n个写着单词的盘子,判断能否按顺序摆放盘子,使每个盘子的首尾字母相同(除了第一个和最后一个盘子)。

题目链接:Play on Words

解题思路:

就是我们小时候玩的“一笔画”问题,不过这有个专业的名词--欧拉路径。

定理1:存在欧拉路径的条件:图是连通的,且存在0个或2个奇点。如果存在2个奇点,则欧拉路一定是从一个奇点出发,以另一个奇点结束。
定理2:存在欧拉回路的条件:图是连通的,且不存在奇点。(网上都解释得挺清楚的)


首先将每个字母数字化,方便处理。

记录每个字母以单词的第一个和最后一个出现的次数,为了判断它的奇点个数。

把出入度不同的顶点的字母单独取出来,只有起点和终点的出入度不同,而且差的绝对值为1.

还要用一个vis数组,来标记这个字母是否出现过,要保证所有出现的字母都是相连的,图才连通,程序上的表达就是根为本身的点只有一个!

最后还是并查集的使用----基本套模板了,第一次发现写程序可以这么死~


代码:

//Play on Words  HDU1116
#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      

using namespace std;
int father[30],in[30],out[30],vis[30],p[30];
char s[1005];
int find_father(int x)  //并查集之查找根节点
{
   /* if(x!=father[x])
        father[x]=find_father(father[x]);
    return father[x];*/
    return x==father[x]?x:father[x]=find_father(father[x]);
}
void unite(int a,int b) //并查集之合并两个集合
{
    int x,y;
    x=find_father(a),y=find_father(b);
    if(x!=y) father[x]=y;
}
int main()
{
    int T,n,i,j,len,cnt,a,b;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(i=0;i<26;i++) {
            father[i]=i,in[i]=out[i]=0,vis[i]=0; //初始化,每个节点的根就是本身,入度和初度都为0,且没有访问过
        }
        for(i=0;i
      
      
       
       >s;
            len=strlen(s);
            a=s[0]-'a',b=s[len-1]-'a';  //将字符串转移到数字的表示处理
            unite(a,b);                  //,b分别表示这个单词的首位字母,所以是连通的
            in[a]++,out[ b]++;         //以此字母为起点或者中点的单词数加1
            vis[a]=1,vis[b]=1;          //这个字母出现过
        }
        for(i=0;i<26;i++)
            find_father(i);      //为每个字母找到其根结点
        for(cnt=0,i=0; i< 26 && cnt<2 ;i++){
            if(vis[i] && father[i]==i) cnt++; //统计路径个数
        }
        if(cnt > 1){                          //有多条路径,即两个图,无法连通
            printf("The door cannot be opened.\n");
            continue;
        }
        for(i=0,cnt=0;i<26;i++){
            if(vis[i] && in[i] != out[i])  //统计入度不等于出度的点的个数并记录
                p[cnt++]=i;
        }
        if(!cnt){                           //每个点的入度都等于出度,是个欧拉回路
            printf("Ordering is possible.\n");
        }
        else if(cnt == 2 &&( ( out[p[0]]-in[p[0]]==1 && in[p[1]]-out[p[1]]==1)
            ||   (out[p[1]]-in[p[1]]==1 && in[p[0]]-out[p[0]]==1 ) ))  //有且只有两个出入度不同的点,即起点和终点,并且出入度差为1,是欧拉路径
            {
                printf("Ordering is possible.\n");
            }
        else                                    //除了欧拉回路和欧拉路径,就无法使每个单词首位相连有解
            printf("The door cannot be opened.\n");

    }
    return 0;
}

      
      
     
     
    
    
   
   





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值