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 ...
随机推荐
- 【BZOJ1834】[ZJOI2010]network 网络扩容 最大流+最小费用流
[BZOJ1834][ZJOI2010]network 网络扩容 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不 ...
- [IIS] IIS Framework "aspnet_regiis.exe" 注册
Error:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“
- OC开发_Storyboard——多线程、UIScrollView
一.多线程 1.主队列:处理多点触控和所有UI操作(不能阻塞.主要同步更新UI) dispatch_queue_t mainQueue = dispatchg_get_main_queue(); // ...
- [Android Tips] 31.如何将第三库引入的 Permission 删除掉
Just declare the incriminated permission in your (main) Manifest with the tools:node="remove&qu ...
- 170808、生成为CVS文件
/** * Desc : 生成为CVS文件 * User : RICK * @param data 源数据List * @param map csv文件的列表头map * @param outPutP ...
- HI3518E用J-link烧写裸板fastboot u-boot流程
Hi3518E的裸板烧写fastboot是不能像HI3531那样,可以通过FB直接烧写.遵循ARM9的烧写流程.其中一般u-boot的烧写流程可分为几类:第一:通过编程器芯片直接烧写:第二通过RVDS ...
- K-均值聚类(K-means)算法
https://www.cnblogs.com/ybjourney/p/4714870.html 最近在看<机器学习实战>这本书,因为自己本身很想深入的了解机器学习算法,加之想学pytho ...
- JSON_EXTRACT查询mysql中的{}和 [{},{}中的值]
json_extract(a.tag, '$[*].tag_name.cn') as tag, json_extract(a.address,'$.en') as address_name, json ...
- talib 中文文档(十一):Cycle Indicator Functions 周期指标
Cycle Indicator Functions 不是很懂,欢迎指教 HT_DCPERIOD - Hilbert Transform - Dominant Cycle Period 函数名:HT_D ...
- Scala数组和集合
一.scala数组 数组定义1: var arr = new Array[String](3) String:存储的元素类型 3:存储3个元素 添加元素: arr(1) = "hello&q ...