传送门

WORDS1 - Play on Words

#graph-theory #euler-circuit

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, equal to about 500) is given on the first line of the input file. Each test case begins with a line containing a single integer number N that 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.".

Example

Sample input:3
2
acm
ibm
3
acm
malform
mouse
2
ok
okSample output:The door cannot be opened.
Ordering is possible.
The door cannot be opened.-----------------------------------------------------题目要求将所有单词排列成such that the first letter of each word is equal to the last letter of the previous word。这道题是典型的欧拉路径问题,这种问题分析起来还是有一定难度的。我们考虑如何建图将26个字母看成节点,将每个单词看成从其首字母向尾字母的有向边。问题转换成判断有向图中是否存在一条欧拉路径。-----------------------------------------------------Solution一个有向图存在欧拉路径的必要条件是:每个节点的出度都等于入度存在一个节点s,其入度比出度少1,存在一个节点t,其出度比入度多1在前一种情况下若图中存在欧拉路径,那么起点和终点必然是同一点,而且任意出度不为0的节点都可作为起点,在后一种情况下若图中存在欧拉路径,那s必然是起点,t必然是终点。-----------------------------------------------------但上述条件仅仅是必要条件,除此之外还要求图“连通”,即要求从前面确定的起点出发可以走完所有边(这好像是废话)其实前面的必要条件仅能快速判断出欧拉路径不存在的情况,对于欧拉路径存在的情况仍然要通过遍历图中的边来确认。------------------------------------------------------下面考虑如何遍历有向图(可能存在重边、自环)中的边。如果用vector<int> g[N存图的话不是很方便,而链式前向星是比较方便的请特别注意下面代码中加粗的三行,并请考虑如果将那三行改成下面两种写法有什么问题
void dfs(int u){for(; ~head[u]; head[u]=E[head[u]].nt){int &v=E[head[u]].to;dfs(v);}
}

void dfs(int u){for(; ~head[u];){int &v=E[head[u]].to;head[u]=E[head[u]].nt;dfs(v);}
}

----------------------------------------------------------Code
#include <bits/stdc++.h>
using namespace std;
char s[1005];
int in[26], out[26];
const int M(1e5+5);
struct edge{int to, nt;
}E[M];
int head[26];
void dfs(int u){for(; ~head[u];){int v=E[head[u]].to;    //this edge has been usedhead[u]=E[head[u]].nt;dfs(v);}
}
bool solve(int n){bool flag=true;for(int i=0; i<26; i++)if(in[i]!=out[i]){flag=false; break;}if(flag){for(int i=0; i<26; i++) if(out[i]){dfs(i); break;}for(int i=0; i<26; i++) if(~head[i]) return 0;return 1;}int s=-1, t=-1;for(int i=0; i<26; i++){if(in[i]!=out[i]){if(abs(in[i]-out[i])!=1) return 0;if(in[i]>out[i]){if(~t) return 0;t=i;}else if(in[i]<out[i]){if(~s) return 0;s=i;} }}if(~s&&~t){dfs(s); for(int i=0; i<26; i++) if(~head[i]) return 0;return 1;}return 0;
}
int main(){int T; scanf("%d", &T);for(int n; T--;){scanf("%d", &n);memset(in, 0, sizeof(in));memset(out, 0, sizeof(out));memset(head, -1, sizeof(head));for(int len, u, v, id=0, _=n; _--;){scanf("%s", s);len=strlen(s);u=s[0]-'a', v=s[len-1]-'a';out[u]++, in[v]++;E[id]={v, head[u]}, head[u]=id++;}puts(solve(n)?"Ordering is possible.":"The door cannot be opened.");}
}

转载于:https://www.cnblogs.com/Patt/p/4820307.html

SPOJ Play on Words相关推荐

  1. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MB Submit: 7669  Solved: 1894 [Sub ...

  2. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

    全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...

  3. SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并

    Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...

  4. SPOJ ATOMS - Atoms in the Lab

    题目链接:http://www.spoj.com/problems/ATOMS/ 题目大意:有N个原子,他们每秒分裂成K个新原子,新原子也能继续分裂.问如果要控制他的数量为M以内,应在什么时候使其停止 ...

  5. SPOJ 375. Query on a tree (树链剖分)

    题目链接: http://www.spoj.com/problems/QTREE/ 375. Query on a tree Problem code: QTREE You are given a t ...

  6. SPOJ 694 Distinct Substrings(后缀数组)

    题目链接:http://www.spoj.com/problems/DISUBSTR/ 题意:给定一个串,求不同的字串的个数. 思路:每个子串一定是某个后缀的前缀,那么原问题等价于求所有后缀之间的不相 ...

  7. spoj Pattern Find(kmp)

    解法1 先计算模式串的前缀函数,然后通过前缀函数来搜索文本串 代码参考: https://github.com/wuli2496/OJ/blob/master/spoj/Pattern%20Find/ ...

  8. SPOJ Pattern Find(Rabin Karp)

    使用Rabin-Karp算法. 代码参考: OJ/spoj/Pattern Find at master · wuli2496/OJ · GitHub

  9. SPOJ Ada and Spring Cleaning(hash)

    思路:先计算出前k个字符组成的子串的哈希值,再根据哈希值及递推关系 具体代码参考: https://github.com/wuli2496/OJ/blob/master/spoj/Ada%20and% ...

  10. SPOJ Substring Problem(Rabin Karp TLE)

    给出一个文本串及n个模式串,检查对应的模式串是否在文本串中出现 使用rabin karp算法超时 代码见: https://github.com/wuli2496/OJ/blob/master/spo ...

最新文章

  1. win7硬件要求_电脑硬件运行游戏测评
  2. 深度学习之四:常用模型和方法
  3. simulink中mpc模块怎么使用_Controllogix系统中ILX34MBS485模块使用
  4. hdu 1027 STL next_permutation
  5. matlab谐波仿真代码,matlab的谐波仿真程序基于ip-iq法???怎么出不来图像啊???...
  6. hdu 4279 Number
  7. Mysql搭建PXC集群 - Percona XtraDB Cluster
  8. input框保持两位小数
  9. 多路查找树之2-3树的删除原理 - 数据结构和算法81
  10. python中最难的是什么_传说中Python最难理解的点|看这完篇就够了
  11. HDU 1269 迷宫城堡 (强连通分量,常规)
  12. java去0,Java如何处理除零?
  13. 人生感悟-是留丰碑还是墓碑
  14. 【C++入门】静态成员详解(定义、实现原理、使用注意事项)
  15. OllyDbg的基本使用
  16. 涂鸦智能平台——mcu+nbiot
  17. 用spss判断正态性检验的几种方法
  18. 时间格式 年月日时分秒毫秒
  19. 【炼丹炉】CentOS 7安装GPU(Tesla P100)驱动
  20. 5款剪辑视频,总有一款是你想要的!

热门文章

  1. mapreduce 多种输入
  2. sublime 3 前端神器详细 安装教程
  3. WinHTTP Web Proxy Auto-Discovery Service
  4. 面向对象的5条基本设计原则
  5. HDU-1002 A + B Problem II Java大数
  6. 企业局域网——论文开题报告
  7. C++字符串与C字符串的相互转换问题
  8. python 获取当前路径_Python获取当前路径实现代码
  9. MySQL incompatible with sql_mode=only_full_group_by 问题解决
  10. 【渝粤教育】国家开放大学2018年秋季 1323T内科护理学(本) 参考试题