POJ 2337 Catenyms
http://poj.org/problem?id=2337
题意:
判断给出的单词能否首尾相连,输出字典序最小的欧拉路径。
思路:
因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会优先加字典序小的边,dfs的时候也就会先走字典序小的边。
判断一下图的连通性以及是否存在欧拉道路。
然后进行深搜寻找欧拉路径,因为欧拉路径时要逆序输出的,所以这里需要先保存起来,最后再次逆序输出即可得到正确的路径。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef long long ull;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; struct Edge
{
int ID;
int v;
int vis;
Edge(){}
Edge(int id,int x,int y):ID(id),v(x),vis(y){}
}; int n;
int p[];
int in[], out[];
string str[maxn]; vector<Edge> G[maxn]; stack<int> sta; int Find(int x)
{
return p[x]==x?x:p[x]=Find(p[x]);
} void Fleury(int u)
{
for(int i=;i<G[u].size();i++)
{
if(!G[u][i].vis)
{
G[u][i].vis=;
Fleury(G[u][i].v);
sta.push(G[u][i].ID); //这儿是逆序存储
}
}
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
memset(in,,sizeof(in));
memset(out,,sizeof(out)); scanf("%d",&n);
for(int i=;i<;i++) {G[i].clear();p[i]=i;} for(int i=;i<n;i++) cin>>str[i];
sort(str,str+n); //排序,按照字典序顺序加边,这样等下dfs的时候就会先选择字典序小的边 int start;
for(int i=;i<n;i++)
{
int a=str[i][]-'a';
int b=str[i][str[i].size()-]-'a';
out[a]++;
in[b]++;
int x=Find(a);
int y=Find(b);
if(x!=y) p[x]=y;
G[a].push_back(Edge(i,b,));
if(i==) start=a;
} int cnt=;
int num1=,num2=,num3=;
for(int i=;i<;i++)
{
if((in[i]||out[i]) && p[i]==i) cnt++;
if(in[i]!=out[i])
{
if(out[i]-in[i]==) {start=i;num1++;}
else if(out[i]-in[i]==-) num2++;
else num3++;
}
} if(!num3 && ((num1== && num2==) || (num1== && num2==)) && cnt==)
{
Fleury(start);
cout<<str[sta.top()]; sta.pop();
while(!sta.empty())
{
cout<<"."<<str[sta.top()];
sta.pop();
}
cout<<endl;
}
else {puts("***");continue;}
}
}
POJ 2337 Catenyms的更多相关文章
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- POJ 2337 Catenyms (欧拉回路)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 2149 Descript ...
- POJ 2337 Catenyms(有向图的欧拉通路)
题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...
- POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)
题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串 按照 词语接龙,首尾相接 的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...
- POJ 2337 Catenyms (欧拉图)
本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...
- Poj 2337 Catenyms(有向图DFS求欧拉通路)
题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...
- Day 4 -E - Catenyms POJ - 2337
A catenym is a pair of words separated by a period such that the last letter of the first word is th ...
随机推荐
- pano2vr制作360全景图
1.下载pano2vr中文破解版2.制作360全景选择"矩形球面投影" 3.输出格式选择HTML5, 也可选择Flash(快被淘汰) 4.HTML5输出选项中3个重要选项4.1 F ...
- go练习5--生成md5
import "crypto/md5" import "encoding/hex" //go 生成 md5 func T4_1() { m := md5.New ...
- struts2的action如果返回null会怎样
action return nullresponse里直接写要返回的东西, 返回null,就是说视图不跳转到任何地方,当然就出现空白页面了.如果想出现页面就需要在struts.xml文件里面配置res ...
- python 10分钟入门pandas
本文是对pandas官方网站上<10 Minutes to pandas>的一个简单的翻译,原文在这里.这篇文章是对pandas的一个简单的介绍,详细的介绍请参考:Cookbook .习惯 ...
- Redis集群(一)
redis是单线程,但是一般的作为缓存使用的话,redis足够了,因为它的读写速度太快了. 官方的一个简单测试: 测试完成了50个并发执行100000个请求. 设置和获取的值是一个256字节字符串. ...
- C++ string append方法的常用用法
append函数是向string的后面追加字符或字符串. 1).向string的后面加C-string string s = “hello “; const char *c = “out here “ ...
- C# 生成四位数字字母混合验证码
private static void Rand() { var arr = new List<string>(); ; i < ; i++) { arr.Add(i.ToStrin ...
- xplan-打印执行顺序
-- ------------------------------------------------------------------------------------------------- ...
- Solutions for common Android development problems with the Eclipse IDE- Tutorial
Table of Contents 1. Solving typical Android development problems 1.1. Clean Project 1.2. android.co ...
- Python之numpy基本指令
https://blog.csdn.net/mmm305658979/article/details/78745637 # -*- coding: utf-8 -*- 多加练习才是真 import n ...