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. github开源项目学习-front-end-collect

    About 项目地址 项目预览demo(githubio加载较慢) 开源项目fork自:https://github.com/foru17/front-end-collect 此文章是对此开源项目使用 ...

  2. JS函数参数

    1.js不是面向对象,不可以重载函数.如果两个函数方法名相同,参数不同,那么js加载时后面的函数会覆盖前面的函数. 所以调用函数时只会调用后面的方法. 2.js设置可变参数时,可以用arguments ...

  3. APPIUM 输入中文 之套路

    java  appium 在Capabilities 中添加 capabilities.setCapabilities("unicodeKeyboard",true);和 capa ...

  4. Spark Submit 脚本

    当我们需要命令行传递参数时候,将--class 写在前面,然后是jar 最后是参数 spark-submit --master yarn --num-executors 3 --executor-me ...

  5. 所有做java开发的都是些垃圾

    所有做java开发的都是些垃圾,再垃圾的框架,只要有人用,对java程序员来说那就是高性能,高可用,解耦的,非常优秀的一款框架.属于吃屎都吃的津津有味.java里的框架都是垃圾,连一个不错的都没有.比 ...

  6. webservice中jaxws:server 和jaxws:endpoint的区别

    今天在学习使用spring+cxf发布webservice时遇到个问题,我原来是用 <jaxws:endpoint id="helloWorld" implementor=& ...

  7. 打开safari开发者选项

    1.点击Safari启动浏览器 2.点击左上Safari标志,选择偏好设置 3.选择高级,勾选下方的在菜单栏显示开发菜单. 如此,Safari就出现了开发菜单,右键网页元素也会出现查看元素功能了.

  8. ant安装以及环境变量配置、验证

    (一)安装 ant 下载地址: http://ant.apache.org/     根据自己电脑下载对应版本 下载完成以后,可自行解压到自己常用的盘中,但是要记住解压到哪里了,以便后续的环境变量配置 ...

  9. 波涛1202wm8833 lihomme/历织造 2013秋装全新男装夹克 整身年龄外套潮流立领男士休闲外套薄_9才号

    波涛1202wm8833 lihomme/历织造 2013秋装全新男装夹克 整身年龄外套潮流立领男士休闲外套薄_9才号 波涛1202wm8833lihomme/历织造2013秋装全新男装夹克整身年龄外 ...

  10. ★RFC标准库_目录链接

    RFC(Request For Comments)是一个国际标准化的数据库,记录了从计算机到互联网的海量标准协议.它是一个免费公开的IT标准文件分享平台,其内容也在不断增长,与时俱进.它与ISO等组织 ...