传送门

WORDS1 - 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

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
ok Sample 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[];
int in[], out[];
const int M(1e5+);
struct edge{
int to, nt;
}E[M];
int head[];
void dfs(int u){
for(; ~head[u];){
int v=E[head[u]].to; //this edge has been used
head[u]=E[head[u]].nt;
dfs(v);
}
}
bool solve(int n){
bool flag=true;
for(int i=; i<; i++)if(in[i]!=out[i]){flag=false; break;}
if(flag){
for(int i=; i<; i++) if(out[i]){dfs(i); break;}
for(int i=; i<; i++) if(~head[i]) return ;
return ;
}
int s=-, t=-;
for(int i=; i<; i++){
if(in[i]!=out[i]){
if(abs(in[i]-out[i])!=) return ;
if(in[i]>out[i]){
if(~t) return ;
t=i;
}
else if(in[i]<out[i]){
if(~s) return ;
s=i;
}
}
}
if(~s&&~t){
dfs(s);
for(int i=; i<; i++) if(~head[i]) return ;
return ;
}
return ;
}
int main(){
int T; scanf("%d", &T);
for(int n; T--;){
scanf("%d", &n);
memset(in, , sizeof(in));
memset(out, , sizeof(out));
memset(head, -, sizeof(head));
for(int len, u, v, id=, _=n; _--;){
scanf("%s", s);
len=strlen(s);
u=s[]-'a', v=s[len-]-'a';
out[u]++, in[v]++;
E[id]={v, head[u]}, head[u]=id++;
}
puts(solve(n)?"Ordering is possible.":"The door cannot be opened.");
}
}

SPOJ Play on Words的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  2. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  3. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  4. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  5. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  6. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  7. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  8. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

  9. SPOJ GSS2 Can you answer these queries II

    Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Being a ...

  10. SPOJ 3273

    传送门: 这是一道treap的模板题,不要问我为什么一直在写模板题 依旧只放代码 Treap 版 //SPOJ 3273 //by Cydiater //2016.8.31 #include < ...

随机推荐

  1. 替换空格-请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

    class Solution { public: void replaceSpace(char *str,int length) { char *tmp; ; int i; ;i<length; ...

  2. 20135208 第一次JAVA实验报告

    课程:Java程序设计          班级: 1352 姓名:贺邦                学号:20135208 成绩:             指导教师:娄嘉鹏       实验日期:2 ...

  3. Linux(9.28-10.4)学习笔记

    三种数字表示 无符号数: 基于传统的二进制表示法,表示大于或者等于零的数字. 补码(有符号数): 表示有符号数整数的最常见的方式,有符号数就是只可 以为正或者为负的数. 浮点数: 表示实数的科学计数法 ...

  4. 再次遇到\r\n转\r问题

    帮助小伙伴做jenkins的环境搭建.以为5分钟的事情,但是发现了一个诡异的问题.总是提示SVN的url不合法“URL '%s' is not properly URI-encoded”. 由于选择了 ...

  5. Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tool

    重装操作系统后,要重新配置Android开发环境.配置成功后,添加原本项目时却出现了错误! Android requires compiler compliance level 5.0 or 6.0. ...

  6. java 中Handler 和Runnable 的使用 异步发送消息 转

    public class MainActivity extends Activity { TextView text1, text2; Button button; Thread th; @Overr ...

  7. 你所不知道的Python奇技淫巧

    有时候你会看到很Cool的Python代码,你惊讶于它的简洁,它的优雅,你不由自主地赞叹:竟然还能这样写.其实,这些优雅的代码都要归功于Python的特性,只要你能掌握这些Pythonic的技巧,你一 ...

  8. php利用递归函数实现无限级分类

    相信很多学php的很多小伙伴都会尝试做一个网上商城作为提升自己技术的一种途径.各种对商品分类,商品名之类的操作应该是得心应手,那么就可以尝试下无限级分类列表的制作了. 什么是无限级分类? 无限级分类是 ...

  9. Node基础:域名解析DNS(ok)

    写在前面 Nodejs学习手册,基础总结之DNS模块.对从事web开发的同学来说,DNS解析再熟悉不过,在nodejs中也有一个模块可以完成dns解析的工作,使用非常简单.直接进入主题. 域名解析:d ...

  10. http请求过程简要

    一次http请求主要分为3个大步. 建立tcp连接. 这里就发生了经典的tcp三次握手.做个类比解释下,tcp好比http的秘书,和厂家(服务器端)做买卖.老板(http)叫秘书(tcp)去联系一下, ...