Play on Words UVA - 10129
题目:
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.
题目大意翻译:
有一些秘密的门包含着非常有趣的单词迷题, 考古学家队伍必须解决它们才能够打开大门。 因为没有其他方法能偶打开这些门, 所以解决那些迷题对我们非常重要。
在每个门上有很多个有磁力的盘子,盘子上面写着单词。 必须重新移动放置这些盘子,让它们形成一个队列:队列中,除了第一个单词,每个单词的开头和上一个单词的结尾字母
一样。例如, motorola的后面可以接上acm。
你的任务是写一个程序, 读入一系列单词,然后计算确定它们是否有可能被排成这样的队列。
样例输入:
3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok
样例输出:
The door cannot be opened.
Ordering is possible.
The door cannot be opened.
开始用dfs写的,结果因为数据太大,时间超限,然后再网上搜了下才知道是用欧拉回路加dfs做的
这是我的dfs代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char mapn[][];
int flag,n,vis[];
void dfs(char c,int a)
{
if( a == n)
{
flag = ;
return;
}
for(int i=;i<n;i++)
{
if(mapn[i][] == c && !vis[i])
{
vis[i] = ;
dfs(mapn[i][strlen(mapn[i])-],++a);
vis[i] = ;
}
}
return;
}
int main()
{
int t;
cin >> t;
while(t--)
{
flag = ;
memset(vis,,sizeof(vis));
memset(mapn,,sizeof(mapn));
cin >> n;
for(int i=;i<n;i++)
cin >> mapn[i];
for(int i=;i<n;i++)
{
vis[i] = ;
dfs(mapn[i][strlen(mapn[i])-],);
vis[i] = ;
}
if(flag)
cout << "Ordering is possible." << endl;
else cout << "The door cannot be opened." << endl;
}
return ;
}
下面这个是用欧拉回路加dfs做的
#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
#define MAXN 100
using namespace std;
int vis[MAXN],G[MAXN][MAXN],N, T, in[MAXN],out[MAXN];
void dfs(int u)//只用dfs会时间超限
{
vis[u] = true;
for(int i=; i<MAXN; ++i)
{
if(G[u][i] && !vis[i])
dfs(i);
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(G, , sizeof(G));
memset(in, , sizeof(in));
memset(out, , sizeof(out));
char str[];
scanf("%d",&N);
for(int i=; i<N; ++i)
{
scanf("%s", str);
++G[str[]-'a'][str[strlen(str)-]-'a'];//存下每个单词的开头结尾好搜索
++out[str[]-'a'];//出度
++in[str[strlen(str)-]-'a'];//入度
}
bool flag=true;
int num1=, num2=;
for(int i=; i<MAXN; ++i)
{
if(!flag)
break;
/*对于有向图, 则必须其中一个点的出度恰好比入度大1,
另一个的入度比出度大。
如果奇点数不存在的话,
则可以从任意点出发,最终一定会回到该点(成为欧拉回路)。*/
if(in[i]!=out[i])
{
if(in[i]==out[i]+)
{
++num1;
}
else if(out[i]==in[i]+)
{
++num2;
}
else
{
flag=false;
break;
}
}
}
if(num1 && num2 && num1+num2>)
flag=false;
if(flag)
{
memset(vis, , sizeof(vis));
for(int i=; i<MAXN; ++i)
if(out[i])
{
dfs(i);
break;
}
//搜索判断是否构成回路
bool flag2=true;
for(int i=; i<MAXN; ++i)
{
if(in[i] && !vis[i])
{
flag2=false;
break;
}
if(out[i] && !vis[i])
{
flag2=false;
break;
}
}
if(flag2) printf("Ordering is possible.\n");
else printf("The door cannot be opened.\n");
}
else
{
printf("The door cannot be opened.\n");
}
}
return ;
}
Play on Words UVA - 10129的更多相关文章
- Play on Words UVA - 10129 欧拉路径
关于欧拉回路和欧拉路径 定义:欧拉回路:每条边恰好只走一次,并能回到出发点的路径欧拉路径:经过每一条边一次,但是不要求回到起始点 ①首先看欧拉回路存在性的判定: 一.无向图每个顶点的度数都是偶数,则存 ...
- Uva 10129 单词
题目链接:https://uva.onlinejudge.org/external/101/10129.pdf 把单词的首字母和最后一个字母看做节点,一个单词就是一个有向边.有向图的欧拉定理,就是除了 ...
- UVa 10129单词(欧拉回路)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Uva 10129 - Play on Words 单词接龙 欧拉道路应用
跟Uva 10054很像,不过这题的单词是不能反向的,所以是有向图,判断欧拉道路. 关于欧拉道路(from Titanium大神): 判断有向图是否有欧拉路 1.判断有向图的基图(即有向图转化为无向图 ...
- uva 10129 play on words——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABNUAAANeCAYAAAA1BjiHAAAgAElEQVR4nOydabWsuhaFywIasIAHJK
- UVa 10129 (并查集 + 欧拉路径) Play on Words
题意: 有n个由小写字母的单词,要求判断是否存在某种排列使得相邻的两个单词,前一个单词末字母与后一个单词首字母相同. 分析: 将单词的两个字母看做节点,则一个单词可以看做一条有向边.那么题中所求的排列 ...
- UVa 10129 Play On Words【欧拉道路 并查集 】
题意:给出n个单词,问这n个单词能否首尾接龙,即能否构成欧拉道路 按照紫书上的思路:用并查集来做,取每一个单词的第一个字母,和最后一个字母进行并查集的操作 但这道题目是欧拉道路(下面摘自http:// ...
- UVA - 10129 Play on Words(欧拉回路+并查集)
2.解题思路:本题利用欧拉回路存在条件解决.可以将所有的单词看做边,26个字母看做端点,那么本题其实就是问是否存在一条路径,可以到达所有出现过的字符端点.由于本题还要求了两个单词拼在一起的条件是前一个 ...
- UVA 10129 Play on Words
欧拉回路 以字母为结点,单词为边:注意两个相同的单词表示两条边. 并查集判断是否连通,出度,入度判断是否是欧拉回路 #include <iostream> #include <cst ...
- uva 10129 poj 1386 hdu 1116 zoj 2016 play on words
//本来是想练一下欧拉回路的,结果紫书上那题是大水题!!!!! 题意:给出n个单词,是否可以把单词排列成每个单词的第一个字母和上一个单词的最后一个字母相同 解:欧拉通路存在=底图联通+初度!=入度的点 ...
随机推荐
- Spring源码解析——循环依赖的解决方案
一.前言 承接<Spring源码解析--创建bean>.<Spring源码解析--创建bean的实例>,我们今天接着聊聊,循环依赖的解决方案,即创建bean的ObjectFac ...
- 【JDK】JDK源码分析-AbstractQueuedSynchronizer(1)
概述 前文「JDK源码分析-Lock&Condition」简要分析了 Lock 接口,它在 JDK 中的实现类主要是 ReentrantLock (可译为“重入锁”).ReentrantLoc ...
- S2:.net
1.net框架结构 主要包含公共语言运行时(CLR)和框架类库(.NET Framework 类库 ,FCL) 2.CLR 1.对于一个将要面向.NET平台进行开发的人来说,了解一下.NET平台的整 ...
- python_0基础学习_day01
Python是一门动态解释型的强类型定义语言 一.变量 变量命名规则 由数字.字母.下划线组成 不能以数字开头 要具有描述性 要区分大小写 禁止使用python的关键字(在pycharm中关键字明明变 ...
- 夯实Java基础(四)——面向对象之多态
1.多态介绍 面向对象三大特征:封装.继承.多态.多态是Java面向对象最核心,最难以理解的内容.从一定角度来看,封装和继承几乎都是为多态而准备的. 多态就是指程序中定义的引用变量所指向的具体类型和通 ...
- React 如何搭建脚手架
React 如何搭建脚手架 npm install -g create-react-app //安装 create-react-app react-demo // react-demo ...
- JMS入门简介
一.JMS是什么 1.JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中 ...
- 线性分类 Linear Classification
软分类:y 的取值只有正负两个离散值,例如 {0, 1} 硬分类:y 是正负两类区间中的连续值,例如 [0, 1] 一.感知机 主要思想:分错的样本数越少越好 用指示函数统计分错的样本数作为损失函数, ...
- MySQL高速缓存
MySQL高速缓存启动方法及参数详解query_cache_size=32M query_cache_type=1,默认配置下,MySQL的该功能是没有启动的,可能你通过show variables ...
- String关键字
关于String和new String()见我写的前一篇博客 String和new String()的区别 1.String的"+"运算 a.String str = " ...