POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8756 | Accepted: 2306 |
Description
dog.gopher
gopher.rat
rat.tiger
aloha.aloha
arachnid.dog
A compound catenym is a sequence of three or more words separated by periods such that each adjacent pair of words forms a catenym. For example,
aloha.aloha.arachnid.dog.gopher.rat.tiger
Given a dictionary of lower case words, you are to find a compound catenym that contains each of the words exactly once.
Input
Output
Sample Input
2
6
aloha
arachnid
dog
gopher
rat
tiger
3
oak
maple
elm
Sample Output
aloha.arachnid.dog.gopher.rat.tiger
***
Source
把26个小写字母当成点,每个单词就是一条边。
然后就是求欧拉路径。
为了保证字典序最小,要先排序,加边要按照顺序加。
而且求解的dfs起点要选择下,选择最小的。
/* ***********************************************
Author :kuangbin
Created Time :2014-2-3 13:12:43
File Name :E:\2014ACM\专题学习\图论\欧拉路\有向图\POJ2337.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct Edge
{
int to,next;
int index;
bool flag;
}edge[];
int head[],tot;
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int index)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].index = index;
edge[tot].flag = false;
head[u] = tot++;
}
string str[];
int in[],out[];
int cnt;
int ans[];
void dfs(int u)
{
for(int i = head[u] ;i != -;i = edge[i].next)
if(!edge[i].flag)
{
edge[i].flag = true;
dfs(edge[i].to);
ans[cnt++] = edge[i].index;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i < n;i++)
cin>>str[i];
sort(str,str+n);//要输出字典序最小的解,先按照字典序排序
init();
memset(in,,sizeof(in));
memset(out,,sizeof(out));
int start = ;
for(int i = n-;i >= ;i--)//字典序大的先加入
{
int u = str[i][] - 'a';
int v = str[i][str[i].length() - ] - 'a';
addedge(u,v,i);
out[u]++;
in[v]++;
if(u < start)start = u;
if(v < start)start = v;
}
int cc1 = , cc2 = ;
for(int i = ;i < ;i++)
{
if(out[i] - in[i] == )
{
cc1++;
start = i;//如果有一个出度比入度大1的点,就从这个点出发,否则从最小的点出发
}
else if(out[i] - in[i] == -)
cc2++;
else if(out[i] - in[i] != )
cc1 = ;
}
if(! ( (cc1 == && cc2 == ) || (cc1 == && cc2 == ) ))
{
printf("***\n");
continue;
}
cnt = ;
dfs(start);
if(cnt != n)//判断是否连通
{
printf("***\n");
continue;
}
for(int i = cnt-; i >= ;i--)
{
cout<<str[ans[i]];
if(i > )printf(".");
else printf("\n");
}
}
return ;
}
POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)的更多相关文章
- poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...
- HDU 5915 The Fastest Runner Ms. Zhang (CCPC2016 长春 E题,分类讨论 + 求字典序最小的直径 + 数据结构寻找最小值)
题目链接 CCPC2016 Changchun Problem E 题意 给定一个$n$个点$n$条边的无向图,现在从某一点$s$出发,每个点都经过一遍,最后在$t$点停止,经过的边数为$l$ ...
- [poj2337]求字典序最小欧拉回路
注意:找出一条欧拉回路,与判定这个图能不能一笔联通...是不同的概念 c++奇怪的编译规则...生不如死啊... string怎么用啊...cincout来救? 可以直接.length()我也是长见识 ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- Poj 2337 Catenyms(有向图DFS求欧拉通路)
题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...
- POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)
题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串 按照 词语接龙,首尾相接 的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...
- POJ 2337 Catenyms
http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...
- POJ 2337 Catenyms (欧拉图)
本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...
- POJ 2337 Catenyms(有向图的欧拉通路)
题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...
随机推荐
- CSSOM
概要 狭义的 DOM API 仅仅包含 DOM 树形结构相关的内容. DOM 中的所有的属性都是用来表现语义的属性,CSSOM 的则都是表现的属性. CSSOM 是 CSS 的对象模型,在 W3C 标 ...
- 玩转Hook——Android权限管理功能探讨(二)
距离我上一篇研究ptrace的随笔http://www.cnblogs.com/zealotrouge/p/3544147.html已经过去半年了,最近不忙的时候抽空继续研究了下.同样,参考了Prad ...
- PHP-Redis操作
/*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host,端口为6379,超 ...
- python中的__call__
如果python中的一个类定义了 __call__ 方法,那么这个类它的实例就可以作为函数调用,也就是实现了 () 运算符,即可调用对象协议 下面是一个简单的例子: class TmpTest: de ...
- ruby http爬虫中的 :body 用法问题
require 'http' url = 'http://localhost/b.php' data = 'whoami=whoami' html = HTTP.via('127.0.0.1',808 ...
- springcloud中的负载均衡策略
IRule 这是所有负载均衡策略的父接口,里边的核心方法就是choose方法,用来选择一个服务实例. AbstractLoadBalancerRule AbstractLoadBalancerRule ...
- trove远程连接mongodb
创建数据库 <pre> [root@a581c7388dca /]# trove database-create e50f3b40-5165-4ccc-af9f-c121089fd902 ...
- poj2709
模拟题,在合成灰色的时候,每次取当前剩余最多的三种颜色,各取1mL合成.然后重新看剩余最多的是哪三个. #include <cstdio> #include <cstdlib> ...
- 【windows】在控制面板卸载软件的时候,出现2502,2503的问题
1. 打开“任务管理器”,找到“详细信息”的页签,将“explorer.exe”的进程结束任务 2.菜单栏的“文件”-->"建立新任务"--> 输入Explorer.e ...
- 使用 Gradle 对应用进行个性化定制
啥也不说了,直接进入主题吧.本篇文章主要根据实际开发中遇到的需求,讲解使用 Gradle 对应用的不同版本进行个性化定制. 场景介绍 一般的应用基本上都有正式服和测试服,这个就不需要多说了.但是有些应 ...