Catenyms
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8368   Accepted: 2202

Description

A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For example, the following are catenyms:

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

The first line of standard input contains t, the number of test cases. Each test case begins with 3 <= n <= 1000 - the number of words in the dictionary. n distinct dictionary words follow; each word is a string of between 1 and 20 lowercase letters on a line by itself.

Output

For each test case, output a line giving the lexicographically least compound catenym that contains each dictionary word exactly once. Output "***" if there is no solution.

Sample Input

2
6
aloha
arachnid
dog
gopher
rat
tiger
3
oak
maple
elm

Sample Output

aloha.arachnid.dog.gopher.rat.tiger
***

用DFS实现的
 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <map>
using namespace std;
#define ll long long int
#define INF 5100000
string a[];
int v[];
vector<pair<int,int> >aa[];
vector<int>ab;
int p[];
int d1[];
int d2[];
int find(int x)
{
while(x!=p[x])
x=p[x];
return x;
}
void fun(int x,int eid)
{
for(int i=; i<aa[x].size(); i++)
if(!v[aa[x][i].second])
{
v[aa[x][i].second]=true;
fun(aa[x][i].first,aa[x][i].second);
}
if(eid>)ab.push_back(eid);
}
int main()
{
int tt;
int i,j;
cin>>tt;
for(i=; i<tt; i++)
{
int n;
cin>>n;
memset(d1,,sizeof(d1));
memset(d2,,sizeof(d2));
for(j=; j<; j++)
p[j]=j,aa[j].clear();
for(j=; j<=n; j++)
{
cin>>a[j];
}
sort(a+,a+n+);
for(j=; j<=n; j++)
{
int x=a[j][]-'a'+;
int y=a[j][a[j].length()-]-'a'+;
d1[x]++;
d2[y]++;
aa[x].push_back(make_pair(y,j));
int fx=find(x);
int fy=find(y);
if(fy!=fx)
p[fy]=fx;
}
int sum=;
int start=a[][]-'a'+;
for(j=; j<; j++)
{
if(d1[j]==&&d2[j]==)continue;
if(p[j]==j)sum++;
}
if(sum>)
cout<<"***"<<endl;
else
{
int dd1=,dd2=;
for(j=; j<; j++)
{
if(d1[j]!=d2[j])
{
if(d1[j]-d2[j]==)
dd1++,start=j;
else if(d2[j]-d1[j]==)
dd2++;
else
{
dd2=;
break;
}
}
}
if(dd2<=&&dd1<=)
{
memset(v,,sizeof(v));
ab.clear();
fun(start,);
for( j=ab.size()-; j>=; j--)
{
cout<<a[ab[j]];
if(j!=)cout<<".";
else cout<<"\n";
} }
else cout<<"***"<<endl;
}
}
}

poj2337欧拉回路要求输出路径的更多相关文章

  1. POJ 2230 Watchcow(欧拉回路:输出点路径)

    题目链接:http://poj.org/problem?id=2230 题目大意:给你n个点m条边,Bessie希望能走过每条边两次,且两次的方向相反,让你输出以点的形式输出路径. 解题思路:其实就是 ...

  2. VS 工程的 输出路径和工作路径的区别

    输出路径,是vs编译项目生成可执行文件的路径:工作路径是环境变量,比如我们在程序中写相对路径,就是以这个路径为基础的.在默认情况下,输出路径和工作路径都不写的话,默认是程序的bin下面的debug或者 ...

  3. HD1385Minimum Transport Cost(Floyd + 输出路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. C++builder XE 安装控件 及输出路径

    C++builder XE 安装控件 与cb6不一样了,和delphi可以共用一个包. 启动RAD Studio.打开包文件. Project>Options>Delphi Compile ...

  5. HDU 1385 Minimum Transport Cost (最短路,并输出路径)

    题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...

  6. web项目Log4j日志输出路径配置问题

    问题描述:一个web项目想在一个tomcat下运行多个实例(通过修改war包名称的实现),然后每个实例都将日志输出到tomcat的logs目录下实例名命名的文件夹下进行区分查看每个实例日志,要求通过尽 ...

  7. VJP1071新年趣事之打牌(背包+输出路径)

    简单的01背包 保存下方案总数 其实就是dp[v]值 输出路径dfs一下 #include <iostream> #include<cstdio> #include<cs ...

  8. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. Cmake 脚本对项目输出路径和输出头文件的路径定义

    对Lib项目的统一输出路径以下时解决方案: set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Lib)set(CMAKE_LIBRARY_O ...

随机推荐

  1. 搭建互联网DNS构架

    author:JevonWei 版权声明:原创作品 构建"." DNS."com" DNS及"danran.com",用户通过"电 ...

  2. Docker 集群环境实现的新方式

    近几年来,Docker 作为一个开源的应用容器引擎,深受广大开发者的欢迎.随着 Docker 生态圈的不断建设,应用领域越来越广.云计算,大数据,移动技术的快速发展,加之企业业务需求的不断变化,紧随技 ...

  3. linux为用户配置java环境变量

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt226 一. 解压安装jdk 在shell终端下进入jdk-6u14-linu ...

  4. 关于 String.intern() 的思考

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt399 我看到一个问题 https://segmentfault.com/q/ ...

  5. c# 读取excel数据的两种方法(转)

    转载自:http://developer.51cto.com/art/201302/380622.htm, 方法一:OleDb: 用这种方法读取Excel速度还是非常的快的,但这种方式读取数据的时候不 ...

  6. RadioButtonList控件如何取得选中的值

    1.需求:我现在页面上有放两个单选控件,现在要通过判断不同的单选控件来对页面上的标签进行显示和隐藏操作 2.控件如下 <asp:RadioButtonList ID=" RepeatD ...

  7. Java多线程之Executor、ExecutorService、Executors、Callable、Future与FutureTask

    1. 引子 初学Java多线程,常使用Thread与Runnable创建.启动线程.如下例: Thread t1 = new Thread(new Runnable() { @Override pub ...

  8. 201521123122 《java程序设计》第八周实验总结

    201521123122 <java程序设计>第八周实验总结 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 List中指定元素的删除(题目4- ...

  9. java第九次学习总结

    1. 本周学习总结 2.. 书面作业 1.常用异常 题目5-1 1.1 提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 以前编写的代码经常出现异 ...

  10. 201521123112《Java程序设计》第13周学习总结

    1. 本周学习总结 协议的概念是网络中为了通信而建立的规则,常用的应用层协议有http,ftp等. 测试计算机之间的网络是否连通可以使用ping命令. 可以使用IP+端口号的方法来确定数据包是发给哪个 ...