HDU 1116 Play on Words
2015-08-12 19:54
375 查看
HDU 1116 Play on Words
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.
题意:
输入一大堆单词,要求将这些单词连接(要求:前一个单词的最后一个字母需要和后一个单词的第一个字母相同才能连接),是不是所有的单词能连城一个串。
本题使用并查集(并查集的详细解释在这里)+欧拉(回)路(欧拉回路的解释在这里)
我们可以使用并查集建立一个有向图,然后判断这个图是不是连通的
,如果连通,就满足条件。
判断欧拉路是否存在的方法
有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。
无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的.
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.
题意:
输入一大堆单词,要求将这些单词连接(要求:前一个单词的最后一个字母需要和后一个单词的第一个字母相同才能连接),是不是所有的单词能连城一个串。
本题使用并查集(并查集的详细解释在这里)+欧拉(回)路(欧拉回路的解释在这里)
我们可以使用并查集建立一个有向图,然后判断这个图是不是连通的
,如果连通,就满足条件。
判断欧拉路是否存在的方法
有向图:图连通,有一个顶点出度大入度1,有一个顶点入度大出度1,其余都是出度=入度。
无向图:图连通,只有两个顶点是奇数度,其余都是偶数度的.
[code]#include<iostream> #include <string.h> #include <cstdio> #define N 30 using namespace std; const char *x = "Ordering is possible.", *y = "The door cannot be opened."; int u , in , out , vis ; void make() { for(int i = 0; i < 26; i++) u[i] = i; } int find(int a) { while (a != u[a]) { a = u[a]; } return a; } int main() { #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); #endif int n, m, a, b; int i, j, t; char s[1005]; cin >> n; while(n--) { scanf("%d", &m); memset(u, 0, sizeof(u)); memset(in, 0, sizeof(in)); memset(out, 0, sizeof(in)); memset(vis, 0, sizeof(vis)); make(); for(i = 0; i < m; i++) { scanf("%s", s); a = s[0] - 'a'; b = s[strlen(s) - 1] - 'a'; out[a]++; in[b]++; u[a] = u[b] = find(a); vis[a] = vis[b] = 1; } t = 0; for(i = 0; i < 26; i++) { if (vis[i] && i == u[i]) { t++; } } if (t > 1) { cout << y << endl; continue; } a = 0; b = 0; t = 0; for(i = 0; i < 26; i++) { if (vis[i] && in[i] != out[i]) { if (in[i] == out[i] + 1) { a++; } else if (in[i] == out[i] - 1) { b++; } else { t++; } } } if (t) { cout << y << endl;; continue; } if (a == b && a <= 1) { cout << x << endl; } else { cout << y << endl; } } return 0; }
相关文章推荐
- 快速幂。素数筛选。辗转相除。
- fd to filename
- hdu 1181 变形课 DFS
- JS中如何实现每点击一次按钮,显示一条信息
- win7打不开chm格式文件
- 仿支付宝钱包:带分割线的GridView
- VS2013批量注释快捷键
- 字符统计(结构体)_nice
- 构造方法,文档编写
- 前向星和链式前向星
- HttpURLConnection和HttpClient的用法
- [转载] C++11中的右值引用
- Codeforces232D. Fence
- pdo对象查看错误码之errorCode()和errorInfo()
- linux内核之系统调用
- Factorial Trailing Zeroes
- Java Security安全系列文档翻译笔记————KeyStore、密钥、证书、命令行实战
- Block传值
- Solr基本概念
- LeetCode 123 Best Time to Buy and Sell Stock III