Fork me on GitHub

Uva 10129 - Play on Words

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 /*
 2  *Title: 10129 - Play on Words(玩弄单词)
 3  *Author: xueying
 4  *Submit Time: 2013-4-7 23:10
 5  *Submit Times: 1
 6  *Rank: 324/2344
 7  */ 
 8 
 9 
10 
11 #include<stdio.h>
12 #include<string.h>
13 #define MAXN 1010
14 #define ALNUM 26
15 
16 int word[ALNUM+4][ALNUM+4];
17 int times[ALNUM+4][ALNUM+4];
18 char input[MAXN];
19 int cnt;
20 
21 int Traverse(int current)
22 {
23     int i, j, sum, num;
24     cnt++;
25     for(j=0; j<ALNUM; ++j)
26     {
27         sum = word[current][j];
28         num = times[current][j];
29         if(sum > 0 && num < sum)
30         {
31             times[current][j]++;
32             Traverse(j);
33         }
34     }
35     return 0;
36 }
37 
38 int main()
39 {
40 
41     int T, n, i, j, len, flag, point, list;
42     scanf("%d", &T);
43     while(T--)
44     {
45         scanf("%d", &n);
46         getchar();
47         memset(word, 0, sizeof(word));
48         memset(times, 0, sizeof(times));
49         for(i=0; i<n; ++i)
50         {
51             scanf("%s", input);
52             getchar();
53             len = strlen(input);
54             point = input[0] - 'a';
55             list = input[len-1] - 'a';
56             word[point][list]++;
57         }
58         flag = 0;
59         for(i=0; i<ALNUM; ++i)
60         {
61             for(j=0; j<ALNUM; ++j)
62             {
63                 if(word[i][j] > 0)
64                 {
65                     cnt = 0;
66                     times[i][j]++;
67                     Traverse(j);
68                     if(cnt == n) {flag = 1; break;}
69                     memset(times, 0, sizeof(times));
70                 }
71             }
72             if(flag) break;
73         }
74         if(flag) printf("Ordering is possible.\n");
75         else printf("The door cannot be opened.\n");
76     }
77     return 0;
78 }

解题思路:

题目的输出的是判断情况,而不是要将“接龙”的全部单词输出,这意味着我们只需统计输入的单词的首位字母front和末尾位字母,用一个二维数组word[26][26]存储单词,出现一次word[front-'a'][rear-'a']++,用另外一个二维数组times[26][26]统计在遍历的过程中word[i][j]的使用次数,同时,用cnt统计当前使用过的单词的个数。每种单词都有可能作为开始,直到cnt == n,这意味着每个单词都用到了,这时break出去输入可能,否则继续用另外的单词开始遍历,最坏的情况应该是:(26*26)*100000吧?

 

2013年4月8日19:56:18:

现在再回想的时候,感觉自己的代码应该是错的,在Traverse中每次提取的都是在以current开头时第一次遇到的字符啊?这不会影响吗?

posted @ 2013-04-07 23:36  Gifur  阅读(854)  评论(2编辑  收藏  举报
TOP