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

    就是判断单词能否接龙, 是一个有向图的欧拉道路。

    同样用dfs判断连通性(注意判断基图的连通性) 然后判断只有一个点入度>出度,一个点出度> 入度。

    题目:

    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.
    

    代码:

      1 #include <iostream>
      2 #include <string.h>
      3 #include <cstdio>
      4 #include <cmath>
      5 using namespace std;
      6 
      7 const int maxn = 30;
      8 char words[10000];
      9 int G[maxn][maxn];
     10 int vis[maxn];
     11 int in[maxn];
     12 int out[maxn];
     13 bool judge=false;
     14 
     15 void dfs(int u)
     16 {
     17     vis[u]=1;
     18     for(int i=0;i<26;i++)
     19     {
     20         if(G[u][i] &&!vis[i])
     21         {
     22             dfs(i);
     23         }
     24     }
     25 }
     26 bool DFS()
     27 {
     28     int cnt=0;
     29     for(int i=0;i<26;i++)
     30     {
     31         if( (in[i] ||out[i]) && !vis[i])
     32         {
     33             dfs(i);
     34             cnt++;
     35         }
     36     }
     37     if(cnt>1)
     38     {
     39         return false;
     40     }
     41     else
     42         return true;
     43 }
     44 bool euler()
     45 {
     46     int n1=0,n2=0;
     47     for(int i=0;i<26;i++)
     48     {
     49         if(in[i]-out[i]==1)
     50         {
     51             n1++;
     52             if(n1>1)return false;
     53         }
     54         if(out[i]-in[i]==1)
     55         {
     56             n2++;
     57             if(n2>1)return false;
     58         }
     59         if(abs(out[i]-in[i])>1)
     60             return false;
     61     }
     62     return true;
     63 }
     64 
     65 
     66 int main()
     67 {
     68     int tst;
     69     cin>>tst;
     70     while(tst--)
     71     {
     72         int Nw;
     73         cin>>Nw;
     74         cin.ignore();
     75         memset(words,'',sizeof(words));
     76         for(int i=0;i<Nw;i++)
     77         {
     78             gets(words);
     79             int last = words[strlen(words)-1]-'a';
     80             int first = words[0]-'a';
     81             G[first][last]++; G[last][first]++;
     82             in[first]++;out[last]++;
     83         }
     84         if( ! DFS())
     85         {
     86             cout<<"The door cannot be opened."<<endl;
     87         }
     88         else
     89         {
     90             if(!euler())
     91             {
     92                 cout<<"The door cannot be opened."<<endl;
     93             }
     94             else
     95             {
     96                 cout<<"Ordering is possible."<<endl;
     97             }
     98         }
     99         memset(out,0,sizeof(out));
    100         memset(in,0,sizeof(in));
    101         memset(G,0,sizeof(G));
    102         memset(vis,0,sizeof(vis));
    103     }
    104 
    105     return 0;
    106 }
  • 相关阅读:
    第八篇、UITableView常用功能(左滑出现多个按钮,多选删除等)
    第七篇、hitTest UITabbar中间突出按钮额外增加可点击区域
    第二篇、常用的分类文件
    第一篇、Swift_Textkit的基本使用
    第六篇、git常用的命令
    第五篇、常用的SQL语句和函数介绍
    第四篇、图片轮播查看器
    C# 打开文件或打开文件夹
    HttpHandler使用Session
    C# Response 下载
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3446572.html
Copyright © 2011-2022 走看看