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 ...
随机推荐
- MFC中按钮控件的用法笔记(转)
VC学习笔记1:按钮的使能与禁止 用ClassWizard的Member Variables为按钮定义变量,如:m_Button1:则m_Button1.EnableWindow(true); 使按钮 ...
- C++ 调用C++写的函数库的2种方法之一(显式调用)
一:创建C++ DLL类库,名称:Dll1 1.Dll.h _declspec(dllimport) int add(int a, int b); 2.Dll.cpp // Dll.cpp : 定义 ...
- 【转载】UML类图几种关系的总结
因为有的时候很久不弄UML图,老是忘记几个常见的连接线的意思,这篇完全说转载:UML类图几种关系的总结 在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Real ...
- 疑难杂症--单回话下 WITH(NOLOCK)返回更多数据
场景:某DBA在一个人操作数据库时发现,可提交读事务隔离级别下返回的数据少于未提交读事务隔离级别,确认没有其他事务修改数据. 解决方案1: 将数据查询放入一个新建的表,使用该表查询发现问题被消除. ...
- CentOS6.5安装mysql-5.7.18-1.el6.x86_64.rpm-bundle.tar
本文内容为转载内容,具体作者忘记是谁了,在收藏夹找到的 先去官网(https://dev.mysql.com/downloads/mysql/),在Select Operating System选择R ...
- LEFT JOIN与RIGHT JOIN学习笔记
SELECT COUNT(*) FROM [tbiz_PuzzleBasic] SELECT A.BasicID,A.Name,A.Gender,B.WorkID,B.Company,B.Positi ...
- Web Server 在iis上部署webservice基于oracle
在iis上部署webservice基于oracle 常见错误及解决方案: 原因: 先安装.netframework后安装iis造成的: 解决方案: 1.C:\Windows\Microsoft.NET ...
- kvm虚拟化之kvm虚拟机vnc配置
本文是通过vnc方式访问虚拟主机上的KVM虚拟机. 这里的通过vnc方式访问虚拟机不是在kvm虚拟机安装配置vnc服务器,通过虚拟主机的IP地址与端口进行访问,kvm虚拟化对vnc的支持相对来说 ...
- CF223C【Partial Sums】(组合数学+乱搞)
题面 传送门 题解 orz zzk 考虑这东西的组合意义 (图片来自zzk) \(a_i\)这个元素对\(k\)阶前缀和的第\(j\)个元素\(s_{k,j}\)的贡献就等于从\((0,i)\)走到\ ...
- NSUserdefault读书笔记
作用 用来存储首选项的.本来首选项是存在磁盘上的,NSUserdefault相当于提供了一个缓存,不用每次都写文件.也就是说设置首选项以后,可以马上读出来,不必先写到磁盘中去. 定期调用synchro ...