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 ...
随机推荐
- MySQL主从宕机的解决方法
测试系统:centos6.5系统 测试环境IP地址划分: master: 192.168.80.130 slave:192.168.80.143 slave:192.168.80.146 首先模拟(M ...
- Linux mysql-5.7.17安装 教程
1.下载安装文件 #mkdir /data #mkdir /data/software #cd /data/software #wget http://dev.mysql.com/get/Downl ...
- C++操作符重载总结operator(小结 更新ing)
操作符重载一个原则是:被操作的数据类型中 至少有一个是自定义的类型(使用class定义类),如果被操作的变量都是基本类型,那么就不能定义操作符重载. 1.如果重载了new,则必须重载delete,因为 ...
- jvm 虚拟机的组成部分
1.类加载子系统 :负责从文件系统或者网络中加载 Class 信息,加载的信息存放在 一块称之为方法区的内存空间 2.方法区:存放类信息,常量信息,常量池信息,包括字符串字面量和数字常量等 3.Jav ...
- MySQL事件调度器Event Scheduler
我们都知道windows的计划任务和linux的crontab都是用来实现一些周期性的任务和固定时间须要运行的任务. 在mysql5.1之前我们完毕数据库的周期性操作都必须借助这些操作系统实现. 在m ...
- bzoj1082: [SCOI2005]栅栏(二分答案搜索判断)
1082: [SCOI2005]栅栏 题目:传送门 题解: 是不是一开始在想DP?本蒟蒻也是qwq,结果很nice的错了ORZ 正解:二分+搜索 我们可以先把两种木材都进行排序,那么如果需要的最大木材 ...
- php设计模式适配器模式
php设计模式适配器模式 简介 适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的.一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起. 其实就是通过一个转换类,这个转 ...
- nyoj--1237--最大岛屿(dfs+数据处理)
最大岛屿 时间限制:1000 ms | 内存限制:65535 KB 难度: 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战 ...
- Oracle11g数据库导入Oracle10g数据库操作笔记
一.在11g服务器上,使用expdp命令备份数据 EXPDP USERID='SYS/sys@daggis as sysdba' schemas=oa directory=DATA_PUMP_DIR ...
- 你不知道的JavaScript(十一)函数参数
JavaScript函数的定义和使用非常简单,我们看一下下面的代码: <script type="text/javascript"> var sum = functio ...