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的更多相关文章

  1. POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)

    题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...

  2. poj 2337 Catenyms 【欧拉路径】

    题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...

  3. POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8756   Accepted: 2306 Descript ...

  4. POJ 2337 Catenyms (欧拉回路)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8173   Accepted: 2149 Descript ...

  5. POJ 2337 Catenyms(有向图的欧拉通路)

    题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...

  6. POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)

    题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串  按照 词语接龙,首尾相接  的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...

  7. POJ 2337 Catenyms (欧拉图)

    本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...

  8. Poj 2337 Catenyms(有向图DFS求欧拉通路)

    题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...

  9. 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 ...

随机推荐

  1. log file sync 事件(转)

    log file sync   log file sync等待时间发生在redo log从log buffer写入到log file期间. 下面对log file sync做个详细的解释.   何时发 ...

  2. OC开发_Storyboard——视图控制生命周期以及NSNotifications

    一.生命周期 1.ViewDidLoad: 一般的初始化,除了几何图形的初始化(这个时候还没确定) 2.ViewWillAppear: 代表你的视图将要在屏幕上显示,可能会调用多次,对不可见时可能能改 ...

  3. JavaScript获取地址栏的参数!

    第一种方式:手动解析 “location”对象 console.log(window.location); 使用这个对象做跳转: window.location.href=""; ...

  4. LISTAGG

    LISTAGG(measure_expr [, 'delimiter']) WITHIN GROUP (order_by_clause) [OVER query_partition_clause] S ...

  5. vue视频: 自定义指令 && 拖拽 && 自定义键盘信息

    v-textv-forv-html 指令: 扩展html语法 自定义指令:1. 自定义属性指令: Vue.directive(指令名称,function(参数){ this.el -> 原生DO ...

  6. 04.ActiveMQ与Spring JMS整合

        SpringJMS使用参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html ...

  7. Win10安装软件时出现2502、2503错误代码的问题

    主要是权限不够,C:\Windows\temp先访问权限 找到该目录,选择temp文件夹,右键弹出快捷菜单,选择“管理员取得所有权”.确定,OK. 再安装软件OK.

  8. Nginx配置ssl安全证书

    server { listen 443; server_name www.loaclhost.com; ssl on; root /web; ssl_certificate /data/ssl/xxx ...

  9. Oracle Schema Objects——Index

    索引主要的作用是查询优化. Oracle Schema Objects 查看执行计划的权限:查看执行计划plustrace:set autotrace trace exp stat(SP2-0618. ...

  10. wireshark udp 序列号 User Datagram Protocol UDP

    序列号等差2^8固定首部20字节首部20+4字节数据部分1378字节片偏移0位Quick UDP Internet