TOJ 1836 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.
输入
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.
输出
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.".
样例输入
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
样例输出
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
把这题转化成欧拉回路问题就可以了。。。
首先把每一行的第一个字母当做有向图的起始位置,结束的字母当做有向图的结束位置
那么就是 a->m m->m
m->e
=>
a->m->e
再来看看这样算不合法的
o->k
o->k
就不合法了,o的出度为2,入度为0,k的入度为2,出度为0
a->m
i->m
也不合法,m的入度为2,出度为0
该问题可以转换为欧拉回路问题。
1,保证改图是连通的。
2,如果它是环形,那么它的每一个结点的出度=入度。
3,如果它是链状,那么只有它的起始结点(出度-入度=1)和结束结点(入度-出度=1)
#include <stdio.h>
#include <string.h>
int a[30];
int flag[30];
int abs(int a){
if(a<0)return -a;
else return a;
}
int find(int x){
int temp=a[x];
while(temp!=a[temp]){
temp=a[temp];
}
int v;
while(x!=a[x]){
v=a[x];
a[x]=temp;
x=v;
}
return temp;
}
void u(int x,int y){
x=find(x);
y=find(y);
if(x!=y){
a[x]=y;
}
}
int main(int argc, char *argv[])
{
int t;
int begin,end;
char ch[1001];
scanf("%d",&t);
while(t--){
int n;
int in[30],out[30];
scanf("%d",&n);
memset(flag,0,sizeof(flag));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0; i<=25; i++)a[i]=i;
while(n--){
scanf("%s",ch);
int len=strlen(ch);
begin=ch[0]-'a';
end=ch[len-1]-'a';
flag[begin]=1;
flag[end]=1;
in[end]++;
out[begin]++;
u(begin,end);
}
int s=0;
for(int i=0; i<=25; i++){
if(flag[i]==1 && a[i]==i)s+=1;
}
if(s==1){
int f_in=0,f_out=0;
int k;
for( k=0; k<=25; k++){
if(flag[k]==1){
if(in[k]==out[k]){
continue;
}else if(abs(in[k]-out[k])==1){
if(in[k]-out[k]<0)f_out++;
if(in[k]-out[k]>0)f_in++;
if(f_out>1 || f_in>1)break;
}else if(abs(in[k]-out[k])>1){
break;
}
}
}
if(k==26){
puts("Ordering is possible.");
}else{
puts("The door cannot be opened.");
}
}else{
puts("The door cannot be opened.");
}
}
return 0;
}
TOJ 1836 Play on Words的更多相关文章
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- poj 1836 Alignment(dp)
题目:http://poj.org/problem?id=1836 题意:最长上升子序列问题, 站队,求踢出最少的人数后,使得队列里的人都能看到 左边的无穷远处 或者 右边的无穷远处. 代码O(n^2 ...
- POJ 1836 Alignment 水DP
题目: http://poj.org/problem?id=1836 没读懂题,以为身高不能有相同的,没想到排中间的两个身高是可以相同的.. #include <stdio.h> #inc ...
- TOJ 1702.A Knight's Journey
2015-06-05 问题简述: 有一个 p*q 的棋盘,一个骑士(就是中国象棋里的马)想要走完所有的格子,棋盘横向是 A...Z(其中A开始 p 个),纵向是 1...q. 原题链接:http:// ...
- TOJ 1139.Compromise
2015-06-03 问题简述: 大概就是输入两段文本(用小写英文字母表示),分别用#表示一段话的结束输入,输出这两个文本的最长公共子序列. 简单的LCS问题,但是输入的是一段话了,而且公共部分比较是 ...
- poj 1836 Alignment(线性dp)
题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0 ...
- 优先队列运用 TOJ 4123 Job Scheduling
链接:http://acm.tju.edu.cn/toj/showp4123.html 4123. Job Scheduling Time Limit: 1.0 Seconds Memory ...
- 最小生成树 TOJ 4117 Happy tree friends
链接http://acm.tju.edu.cn/toj/showp4117.html 4117. Happy tree friends Time Limit: 1.0 Seconds Memo ...
- TOJ 4120 Zombies VS Plants
链接:http://acm.tju.edu.cn/toj/showp4120.html 4120. Zombies VS Plants Time Limit: 1.0 Seconds Memo ...
随机推荐
- javascript高级程序设计读书笔记----面向对象的程序设计
创建对象 工厂模式 function createPerson(name, age, job){ var o = new Object(); o.name = name; o.age = ...
- C++-结构体,联合体,枚举,的区别
结构体: struct NUM { int number; }a; 结构体是声明只是一个模型,没有分配内存空间.当进行定义结构体变量后才分配内存空间 联合体: union data { int a ...
- delphi 中封装的VCl窗体Tab键响应问题
在DLL中的子窗体不会响应Tab按键的,这个时候就需要手动去指定Tab键的操作,但是前提是主窗体要向这个窗体发送一个消息,一个Tab键按下的消息.基本顺序是这样的: 1. 主窗体用Hook技术捕获Ta ...
- jquery stop(true,false)的意思
stop 是jQuery中用于控制页面动画效果的方法.运行之后立刻结束当前页面上的动画效果.stop在新版jQuery中添加了2个参数:第一个参数的意思是是否清空动画序列,也就是stop的是当前元素的 ...
- 键盘控制背景边框平滑移动(jquery)
今天同事让我看了一个动画效果,是由键盘控制背景边框平滑移动,我感觉挺cool,所以我自己也动手制作了一个.目的是为了锻炼自己,看自己是否也能在短时间内实现. 先上图: 一.html代码 <!DO ...
- Transaction And Lock--两种方式实现可重复读
一些需求要求两次查询数据之间不允许数据被修改,即可重复读取 可重复读REPEATABLE READ与串行化SERIALIZABLE的区别在于串行化要求满足该查询的数据不被修改且无新满足该查询条件的数据 ...
- leetcode 移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 这道题比较好想出来 /** ...
- IIS7 配置ssl证书 多域名绑定443端口
IIS7下多个子域名同时配置https协议,但IIS7默认支持单个443端口造成端口冲突: 解决方案:先把每个域名配置不同的端口 例:444,445,446等 然后在:C:\Windows\syste ...
- Webserver asp配置及伪静态设置
Webserver IIS asp配置及伪静态设置 一.概述: 在Windows Server 2003系统中,用户可以借助IIS 6.0配置基于ASP.PHP.asp.NET等语言的动态Web网站 ...
- 从golang的垃圾回收说起(下篇)
文章来自网易云社区 4 Golang垃圾回收的相关参数 4.1 触发GC gc触发的时机:2分钟或者内存占用达到一个阈值(当前堆内存占用是上次gc后对内存占用的两倍,当GOGC=100时) # 表示 ...