POJ1368 & HDOJ1116 Play on Words(欧拉回路 + 并查集)
来源:互联网 发布:淘宝店铺人群消费层级 编辑:程序博客网 时间:2024/06/05 05:43
Play on Words
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10844 Accepted: 3695
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.
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.".
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
Sample Output
The door cannot be opened.Ordering is possible.The door cannot be opened.
给出n个字符串,首位相连,问你能不能做成成语接龙游戏。
此题的关键就是将字符串转化为边,记录首尾字母。
AC代码:
#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"using namespace std;const int MAXN = 30;int n, par[MAXN], ran[MAXN], in[MAXN], out[MAXN];bool vis[MAXN];int find(int x){if(x == par[x]) return x;return par[x] = find(par[x]);}void merge(int x, int y){x = find(x);y = find(y);if(x == y) return;if(ran[x] > ran[y]) par[y] = x;else {if(ran[x] == ran[y]) ran[y]++;par[x] = y;}}int main(int argc, char const *argv[]){int t;scanf("%d", &t);while(t--) {memset(ran, 0, sizeof(ran));memset(vis, false, sizeof(vis));memset(in, 0, sizeof(in));memset(out, 0, sizeof(out));scanf("%d", &n);char s[1010];for(int i = 0; i < MAXN; ++i)par[i] = i;for(int i = 0; i < n; ++i) {scanf("%s", s);int a = s[0] - 'a', b = s[strlen(s) - 1] - 'a';vis[a] = vis[b] = true;out[a]++, in[b]++;merge(a, b);}int cnt = 0;for(int i = 0; i < 26; ++i) {if(vis[i] && find(i) == i) cnt++;if(cnt > 1) break;}if(cnt > 1) {printf("The door cannot be opened.\n");continue;}int s1 = 0, s2 = 0;bool flag = true;for(int i = 0; i < 26; ++i)if(vis[i] && in[i] != out[i]) {if(out[i] == in[i] + 1) s1++;else if(in[i] == out[i] + 1) s2++;else {flag = false;break;}}if(flag && s1 <= 1 && s2 <= 1) printf("Ordering is possible.\n");else printf("The door cannot be opened.\n");}return 0;}
1 0
- POJ1368 & HDOJ1116 Play on Words(欧拉回路 + 并查集)
- HDU1116-Play On Words 并查集,欧拉回路
- Play on Words(欧拉回路+并查集)
- hdu 1116 Play on Words(并查集+欧拉回路|| 欧拉路径)
- HDOJ 1116-Play on Words【欧拉路径+欧拉回路+并查集】
- [HDU 1116]Play on Words(欧拉回路/欧拉路径+并查集)
- HDU 1116 Play on Words(并查集,欧拉回路)
- POJ1386 Play on Words 欧拉回路[并查集判断连通]
- Play on words 之并查集+欧拉回路解题报告
- poj 1386 Play on Words(欧拉回路&&并查集)(中等)
- UVA10129 POJ1386 HDU1116 ZOJ2016 Play on Words【欧拉回路+并查集】
- poj1386 play on word 欧拉回路+并查集
- poj 1386-Play on Words-并查集+欧拉
- hdoj 1116 Play on Words 【并查集+欧拉】
- hdoj1116 Play on Words
- POJ Play on Words(欧拉回路)
- poj1386 Play on Words (欧拉回路)
- (欧拉回路)Play on Words(P1386)
- window service
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 第四章 Django 基础
- wxpython初学者(一)
- JAVA 基础笔记之四 final参数可以修改吗?
- POJ1368 & HDOJ1116 Play on Words(欧拉回路 + 并查集)
- 第四课 MongoDB 数据查询(一)
- ViewHolder的经典用法
- Ansi,UTF8,Unicode,ASCII编码的区别(转载)
- 使用valgrind检查内存使用问题
- Android学习——Android安装
- SYN 攻击原理以及防范技术
- Android学习——Android界面UI
- 笔试真题解析 TT-2016 研发工程师在线模拟笔试题