把26个小写字母当成点,每个单词就是一条边。

然后就是求欧拉路径。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=;
int n,m,tt;
string s[MAXN];
int ans[MAXN],in[],out[];
struct Edge
{
int to,next;
int id;
bool vis;
}edge[MAXN];
int head[],tot;
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int id)
{
edge[tot].to=v;
edge[tot].next=head[u];
edge[tot].id=id;
edge[tot].vis=;
head[u]=tot++;
}
int cnt;
bool dfs(int u)
{
for(int i=head[u];i!=-;i=edge[i].next)
{
if(!edge[i].vis)
{
edge[i].vis=;
dfs(edge[i].to);
ans[cnt++]=edge[i].id;
}
}
}
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
scanf("%d",&tt);
while(tt--)
{
cnt=;
init();
scanf("%d",&n);
for(i=;i<n;i++) cin>>s[i];
sort(s,s+n);
cl(in),cl(out);
int u,v,st=;
for(i=n-;i>=;i--) //注意前向星是后加入的边先遍历
{
u=s[i][]-'a';
v=s[i][s[i].length()-]-'a';
addedge(u,v,i);
out[u]++;
in[v]++;
if(st>u) st=u;
if(st>v) st=v;
}
int sc1=,sc2=;
for(i=;i<;i++)
{
if(out[i]-in[i]==) //只能从这出发
{
sc1++;
st=i;
}
else if(out[i]-in[i]==-) //只能从这出发
{
sc2++;
}
else if(out[i]-in[i]!=) sc1=; //不是欧拉回路
}
if(!((sc1==&&sc2==)||(sc1==&&sc2==)))
{
printf("***\n");
continue;
}
dfs(st);
if(cnt!=n)//判断是否连通
{
printf("***\n");
continue;
}
for(int i=cnt-;i>=;i--)
{
cout<<s[ans[i]];
if(i>)printf(".");
else printf("\n");
}
}
}

poj 2337 欧拉回路输出最小字典序路径 ***的更多相关文章

  1. ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)

    题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...

  2. POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)

    题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...

  3. poj 1041(欧拉回路+输出字典序最小路径)

    题目链接:http://poj.org/problem?id=1041 思路:懒得写了,直接copy吧:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路.一个无向图存 ...

  4. POJ 2337 欧拉回路

    题意: 如果给出的单词能够首尾相接,请按字典序输出单词,中间要加'.' 否则输出三个"*". 思路: 欧拉回路 记得按字典序排序哦~ 加边的时候要倒着加.(邻接表遍历的时候是反着的 ...

  5. poj 2337 有向图输出欧拉路径

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10186   Accepted: 2650 Descrip ...

  6. HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 Problem Description The Public Peace Commission ...

  7. c4 L3-001 找零钱 (简单01背包-输出最小字典序解(用vector保存当前最优解))

    #include <iostream> #include <algorithm> #include <vector> #include <cstdio> ...

  8. Catenyms POJ - 2337(单词+字典序输出路径)

    题意: 就是给出几个单词 看能否组成欧拉回路或路径  当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点   把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...

  9. TZOJ 5110 Pollutant Control(边数最少最小割最小字典序输出)

    描述 It's your first day in Quality Control at Merry Milk Makers, and already there's been a catastrop ...

随机推荐

  1. IDEA 新建文件默认加入CVS

    是要先add,不过可以设置创建的文件都默认 add的.修改默认值看下图:打开系统设置,找到 Version Control 设置选项: 在 When files are created 选项中选择第二 ...

  2. OpenStack网络指导手册 -基本网络概念

    转自:http://blog.csdn.net/zztflyer/article/details/50441200 目录(?)[-] 以太网Ethernet 虚拟局域网VLANs 子网和地址解析协议S ...

  3. Git引用

    原文: http://gitbook.liuhui998.com/7_3.html git中,分支(branch), 远程跟踪分支(remote-tracking branch)以及标签(tag)都是 ...

  4. Word Search I & II

    Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  5. oracle,mybatis主键自增长

    <insert id="insert" parameterType="resource"> <selectKey resultType=&qu ...

  6. IDEA Error:java: 未结束的字符串文字

    首页 > 编程交流 > 基础篇 > IDEA Error:java: 未结束的字符串文字 201601-25 IDEA Error:java: 未结束的字符串文字   IDEA开发, ...

  7. javascript 事件委托,jq,js模拟事件

    <!DOCTYPE> <html> <head> <title></title> <script src="Scripts/ ...

  8. 使用Servlet和jdbc创建用户登录验证

    首先创建一个用户表,用来保存用户名 密码,并插入几条数据 mysql> desc user;+----------+-------------+------+-----+---------+-- ...

  9. Linux下WebSphereV8.5.5.0 安装详细过程

    自WAS8以后安装包不再区别OS,一份介质可以安装到多个平台.只针对Installation Manager 进行了操作系统的区分 ,Websphere产品介质必须通过专门的工具Install Man ...

  10. 【QT】C++ GUI Qt4 学习笔记3

    菜单界面的实现. 看书上第三章,好长,好多代码.我敲了半天,想看看效果,结果却显示不出来.仔细一看,发现spreadsheet的实现在第四章.郁闷.... 又到官网上下代码,结果居然不能运行.难道是因 ...