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 ...
随机推荐
- ntpdate自动对准时间的脚本
author:headsen chen date: 2018-10-09 19:50:15 #!/bin/bash yum -y install ntpdate /usr/sbin/ntpdate ...
- Testlink在CentOS、windows安装
有幸在CentOS\windows上都安装过Teslink程序,总结一下.如下: 一.CentOS安装: 1.安装包需要: xampp xampp-linux-x64-5.6.3-0-installe ...
- mysql load data导入脚本
# !/bin/bash ############中文说明###################### #本程序的一些提示需要中文支持,如系统没有安装中文包,请先安装:yum -y groupinst ...
- Android - 获取SD卡的内存空间大小
获取SD卡的内存空间大小 //获得SD卡空间的信息 File path=Environment.getExternalStorageDirectory(); StatFs statFs=new Sta ...
- 扫描二维码的实现(barcode) ---- HTML5+
模块:barcode Barcode模块管理条码扫描,提供常见的条码(二维码及一维码)的扫描识别功能,可调用设备的摄像头对条码图片扫描进行数据输入.通过plus.barcode可获取条码码管理对象. ...
- 求全局最小割(SW算法)
hdu3002 King of Destruction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 最小树形图(poj3164)
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 12834 Accepted: 3718 ...
- vue 缓存的keepalive页面刷新数据
用到这个的业务场景是这样的: a页面点击新建列表按钮进入到新建的页面b,填写b页面并点击b页面确认添加按钮,把这些数据带到a页面,填充到列表(数组),可以添加多条, 点击这条的时候进入到编辑页面,确认 ...
- Python默认调用路径
记录个遇到的小问题,防止下次遇到忘记怎么解. 起因:pip安装扩展库时提示安装完成,但是在Python 终端下无法import 现象:终端直接运行python 时提示如下:(2.7.13)然而用/us ...
- pandas的Categorical方法
对于数据样本的标签,如果我们事先不知道这个样本有多少类别,那么可以对数据集的类别列进行统计,这时我们用pandas的Categorical方法就非常快的实现. 1.说明: 你的数据最好是一个serie ...