把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. Nginx 使用 sever 段规则屏蔽恶意 User Agent

    相对于 Apache,Nginx 占用的系统资源更少,更适合 VPS 使用.恶意的 User Agent 无处不在,博客更换到 WordPress 没几天,就被 SPAM(垃圾留言)盯上,又被暴力破解 ...

  2. Response.Redirect()、Server.Execute和Server.Transfer的区别

    1.Response.Redirect(): Response.Redirect方法导致浏览器链接到一个指定的URL. 当Response.Redirect()方法被调用时,它会创建一个应答,应答头中 ...

  3. Oracle 11g 下载|Oracle 11g 官网下载|Oracle 11g 官网下载 带登录用户和密码

    本文转载自 Oracle 11g 下载|Oracle 11g 官网下载|Oracle 11g 官网下载 带登录用户和密码 oracle 下载还需要用户名我自己注册了个方便大家使用下载直接点击提示找不到 ...

  4. java 中for each语句

    [转]java foreach 使用   foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便.   foreach语句是for语句的特殊简化版本 ...

  5. ssh免密码登陆设置

    服务器端 CentOS 6.5下编辑/etc/ssh/sshd_config MacOSx下编辑/etc/sshd_config #开启公钥验证 RSAAuthentication yes Pubke ...

  6. wx.ListCtrl简单使用例子

    效果图: 示例代码: #! /usr/bin/env python #coding=utf-8 import wx import sys packages = [('jessica alba', 'p ...

  7. Selenium2(webdirver)入门之环境搭建(Java版)

    一.开发环境: 1.JDK1.6 2.Eclipse:Version: Kepler Service Release 1,下载地址:http://www.eclipse.org/downloads/ ...

  8. UTF8编码转换(C#)

    例如: UTF8---ISO-8859-1 string string = "这是中文";Encoding utf8 = Encoding.UTF8; Encoding ISO = ...

  9. 4.openstack之mitaka搭建glance镜像服务

    部署镜像服务 一:安装和配置服务 1.建库建用户 mysql -u root -p CREATE DATABASE glance; GRANT ALL PRIVILEGES ON glance.* T ...

  10. LayoutInflater的inflate函数用法详解

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: LayoutInflater inflater=(Layo ...