UVA - 10129 Play on Words
来源:互联网 发布:dsp竞价算法 编辑:程序博客网 时间:2024/05/01 22:27
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 numberNthat indicates the number of plates (1 <= N <= 100000). Then exactlyNlines 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
32acmibm3acmmalformmouse2okok
Output for the Sample Input
The door cannot be opened.Ordering is possible.The door cannot be opened.题意:我们将每一个字母转化成欧拉回路,每一个字母的头和尾,相互连接,可以任意组合,判断最后能不能链接起来;判断欧拉回路的条件:
- 最多只能有两个点的入度不等于出度,
- 而且最终必须是其中一个点的出度恰好比入度大1(把它作为起点),另一个入度比出度大1(把它作为终点)。
- 当然前提条件是:在忽略边的方向后,图必须是连通的
#include <cstdio>#include <cstring>#include <iostream>#include <cmath>#define N 26#define M 10000using namespace std;int vis[N];int in[N], out[N];int edge[N][N];int n, m;void dfs(int u) {vis[u] = 1;for (int i = 0; i < N; i++) {if (vis[i] == 0 && edge[u][i])dfs(i);}}int judge() {int num = 0;for (int i = 0; i < N; i++) {if (in[i] != out[i]) {if (fabs(in[i] - out[i])== 1)num++;elsereturn 0;}}if (num > 2) return 0;else {for (int i = 0; i < N; i++) {if (out[i]) {dfs(i);}}for (int i = 0; i < N; i++) {if (out[i] && vis[i] == 0)return 0;if (in[i] && vis[i] == 0)return 0;}return 1;}}int main() {char a[M];while (scanf("%d",&n) != EOF) {while (n--) {scanf("%d",&m);//getchar();memset(in,0,sizeof(in));memset(out,0,sizeof(out));memset(vis,0,sizeof(vis));memset(edge,0,sizeof(edge));for (int i = 0; i < m; i++) {scanf("%s",a);int len = strlen(a);int u,v;u = a[0] - 'a';v = a[len - 1] - 'a';edge[u][v]++;out[u]++;in[v]++;}int k = judge();if (k) printf("Ordering is possible.\n");elseprintf("The door cannot be opened.\n");}}return 0;}
- uva 10129 - Play on Words
- uva 10129 - Play on Words
- uva 10129 - Play on Words
- UVa 10129 - Play on Words
- UVa 10129 - Play on Words
- UVA 10129 - Play on Words
- uva 10129 - Play on Words
- UVA 10129 Play on Words
- uva 10129 - Play on Words
- uva 10129 Play on Words
- uva 10129 Play on Words
- uva 10129 - Play on Words
- UVA 10129 Play on Words
- UVa 10129 Play on Words
- UVA 10129 - Play on Words
- UVA - 10129 Play on Words
- UVA - 10129 Play on Words
- uva -10129 - Play on Words
- Java web开发Extjs与Spring入门实例
- 如何修改Mac截屏保存路径
- MakeKeyLparam 的简化
- linux鸟哥的私房菜读书笔记【管线命令(pipe)】
- 用jFreeChart制作一个饼状的图形统计图
- UVA - 10129 Play on Words
- 动态规划之最长公共子序列
- 【产品经理】总结产品经理职责和素质
- 如何在win7 中安装windows sdk 7.1
- spring cron表达式
- Oracle外连接中对非连接条件使用(+)的作用
- J2EE的四层结构简介
- UVA10602
- php curl 的几个实例