poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337
题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接。问能否够每一个单词用一次,将全部单词连接,能够则输出字典序最小的序列。
代码:
(bin 神的板子)
#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
using namespace std;
int t;
int n;
string s[1010];
struct Edge
{
int to, next;
int index;
bool flag;
}edge[2010];
int head[300], tot;
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u, int v, int index)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].index = index;
edge[tot].flag = false;
head[u] = tot++;
}
int in[250], out[250];
int cnt;
int ans[1010];
void dfs(int u)
{
for (int i = head[u]; i != -1; i = edge[i].next)
{
if (!edge[i].flag)
{
edge[i].flag = true;
dfs(edge[i].to);
ans[cnt++] = edge[i].index;
}
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
scanf("%d",&n);
for (int i = 0; i < n; i++)
cin >> s[i];
sort(s, s + n);
init();
memset(in,0,sizeof(in));
memset(out, 0, sizeof(out));
int start = 100;
for (int i = n - 1; i >= 0; i--)
{
int u = s[i][0] - 'a';
int v = s[i][s[i].length() - 1] - 'a';
addedge(u,v,i);
out[u]++;
in[v]++;
if (u < start) start = u;
if (v < start) start = v;
}
int cc1 = 0, cc2 = 0;
for (int i = 0; i < 26; i++)
{
if (out[i] - in[i] == 1)
{
cc1++;
start = i;
}
else if (out[i] - in[i] == -1)
cc2++;
else if (out[i] - in[i] != 0)
cc1 = 3;
}
if (!((cc1 == 0 && cc2 == 0) || (cc1 == 1 && cc2 == 1)))
{
printf("***\n");
continue;
}
cnt = 0;
dfs(start);
if (cnt != n)
{
printf("***\n");
continue;
}
for (int i = n-1; i >=0 ; i--)
{
cout << s[ans[i]];
if (i != 0) printf(".");
else printf("\n");
}
}
return 0;
}
poj 2337 Catenyms 【欧拉路径】的更多相关文章
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)
题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串 按照 词语接龙,首尾相接 的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...
- POJ 2337 Catenyms
http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...
- POJ 2337 Catenyms (欧拉回路)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 2149 Descript ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- POJ 2337 输出欧拉路径
太无语了. 这道题做了一整天. 主要还是我太弱了. 以后这个就当输出欧拉路径的模版吧. 题目中的输出字典序最小我有点搞不清楚,看了别人是这么写的.但是我发现我过不了后面DISCUSS里面的数据. 题意 ...
- POJ 2337 Catenyms (欧拉图)
本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...
- POJ 2337 Catenyms(有向图的欧拉通路)
题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...
- Poj 2337 Catenyms(有向图DFS求欧拉通路)
题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...
随机推荐
- Myeclipse关闭JS等文件的验证
点击 window > 右键单击properties,弹出properties界面 然后选择MyEclipse->validation->Excluded Resource下找到不需 ...
- git pull 跟 fetch的区别
今天在公司碰到个问题,公司不使用master分支作为主分支,而使用release分支作为主分支,这就碰到了个问题,也就是当clone一个项目下来的时候,如果master跟release分支有冲突,就不 ...
- UNIX基础【UNIX入门经典】
最早在学校很流行.学生毕业以后就会为公司购买操作系统.导致UNIX流行 UNIX内核: Shell:sh csh ksh 其他组件:
- C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解,即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第二篇.主要针对线性表中的链表 ST ...
- 多本Web前端深度修炼书籍(提供网盘下载链接)
书籍介绍:这本书涵盖了html5新增标签和功能,而且提供了jquerymobile,Phonegap,Sencha Touch框架的介绍和应用,最后还带了一个移动web应用的样例,绝对是移动web开发 ...
- Android中的单位
Android中的单位 1.px 像素(pixels) VGA 480*640像素 (Video Graphics Array) QVGA 240*320像素 (Quarter VGA) HVGA 3 ...
- java mail邮件发送(带附件) 支持SSL
java mail邮件发送(带附件)有三个类 MailSenderInfo.java package mail; import java.util.Properties; import java.ut ...
- [jQuery] 选择器和事件
jQuery选择器 属性选择器 <p>p1</p> <span style="font-size:24px;"></span>< ...
- tf.placeholder类似函数中的形参
tf.placeholder(dtype, shape=None, name=None) 此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的值 参数: dtype:数据类型.常用的是tf.fl ...
- JQuery中的时间和动画
我们知道JavaScript和HTML之间的交互是通过用户操作和浏览器成生成事件来完成的,比如当浏览钱加载完一个HTML文档或用户点击一个按钮都会生成一个事件,虽然利用传统的JavaScript事件可 ...