poj 2337 欧拉回路输出最小字典序路径 ***
把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 欧拉回路输出最小字典序路径 ***的更多相关文章
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- poj 1041(欧拉回路+输出字典序最小路径)
题目链接:http://poj.org/problem?id=1041 思路:懒得写了,直接copy吧:对于一个图可以从一个顶点沿着边走下去,每个边只走一次,所有的边都经过后回到原点的路.一个无向图存 ...
- POJ 2337 欧拉回路
题意: 如果给出的单词能够首尾相接,请按字典序输出单词,中间要加'.' 否则输出三个"*". 思路: 欧拉回路 记得按字典序排序哦~ 加边的时候要倒着加.(邻接表遍历的时候是反着的 ...
- poj 2337 有向图输出欧拉路径
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10186 Accepted: 2650 Descrip ...
- HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 Problem Description The Public Peace Commission ...
- c4 L3-001 找零钱 (简单01背包-输出最小字典序解(用vector保存当前最优解))
#include <iostream> #include <algorithm> #include <vector> #include <cstdio> ...
- Catenyms POJ - 2337(单词+字典序输出路径)
题意: 就是给出几个单词 看能否组成欧拉回路或路径 当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点 把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...
- TZOJ 5110 Pollutant Control(边数最少最小割最小字典序输出)
描述 It's your first day in Quality Control at Merry Milk Makers, and already there's been a catastrop ...
随机推荐
- phpcms新闻详情页上一篇下一篇的实现
在新闻详情页(show.html或show_*.html) 只需要添加类似如下代码即可: <div>上一篇:<a href="{$previous_page[url]}&q ...
- linux下logrotate 配置和理解
对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以使用logrotate 程序用来管理系统中的最新的事件,对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以 ...
- 【GoLang】转载:我为什么放弃Go语言,哈哈
我为什么放弃Go语言 作者:庄晓立(Liigo) 日期:2014年3月 原创链接:http://blog.csdn.NET/liigo/article/details/23699459 转载请注明出处 ...
- 【GoLang】golang runtime 调度原理
参考资料: http://m.yl1001.com/group_article/3231471449287668.htm http://tieba.baidu.com/p/3542454435?sha ...
- Spring框架学习[IoC容器高级特性]
1.通过前面4篇文章对Spring IoC容器的源码分析,我们已经基本上了解了Spring IoC容器对Bean定义资源的定位.读入和解析过程,同时也清楚了当用户通过getBean方法向IoC容器获取 ...
- POJ 3740
http://poj.org/problem?id=3740 这是一道搜索+回溯的题目,也是我第一次接触到回溯. 题意就是找一些行,这些行可以使每一列都只存在一个1. 深搜加回溯: memory:11 ...
- android SDK manager 无法获取更新版本列表
打开SDK Manager---Tools---Options,填入如下代理和端口,勾选选项也如下. 网址:mirrors.neusoft.edu.cn 端口:80 99%是成功的 参考:http:/ ...
- http get vs post
http get vs post GET与POST方法有以下区别:(1) 在客户端,Get方式在通过URL提交数据,数据在URL中可以看到:POST方式,数据放置在HTML HEADER内提交.(2) ...
- SAP ALV显示并打印(非OO方式)
*&---------------------------------------------------------------------* *& Report Z_SD_CPF ...
- CAD2010中文版安装问题记录
CAD2010中文版安装到简体中文语言包的时候提示[错误1606无法访问网络位置setup],直接打开cad2010 提示 [adui18res.dll not found]: 首先,进入注册表(re ...