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. windows环境下配置zookeeper

    环境: windows10 zookeeper版本:3.4.9 1.从官网 http://mirror.bit.edu.cn/apache/zookeeper/下载对应的版本 2.将下载的文件解压到任 ...

  2. Linq--一个集合中查找另一个集合,需熟悉这种写法

    //获取科室与病区授权的护士信息        public List<SYS_ZGKSBQDYK> GetUserWardMapByWardCode(string wardCode)   ...

  3. Mysql分页处理(PageHelper)

    第一次做分页处理(完全不知道分页处理到底要做成什么样?) 理解:其实就是前台页面通过传递不同的参数{1.查询的条件.2.查询页数(pageNum),3每页展现的条数(pageSize)},之后我们(只 ...

  4. .Netcore之日志组件Log4net、Nlog性能比较

    转载请注明出处http://www.cnblogs.com/supernebula/p/7506993.html .Netcore之Log4net.Nlog性能比较 最近在写一个开源.netcore ...

  5. 根据本周本月本日来查询数据 C#winform数据查询

    这个我是在winform的页面上做的 1. 首先是在页面上添加3个lable   第一次点击lable会有相应的数据被查询出来  第二次点击同一个lable会刷新所有的数据 2.点击不同的label会 ...

  6. 附录:MySQL忘记root密码

    中小型规模网站集群架构:MySQL忘记root密码 : 矮哥linux运维群:93324526 前言 你忘记系统root密码的时候,你怎么解决的? 不就是single用户进行修改密码吗?这里原理是类似 ...

  7. MySQL (七)--视图、数据库备份和还原

    1 视图 视图:View,是一种有结构(有行有列)但是没结果(结构中不真实存放的数据)的虚拟表,虚拟表的结构来源不是自己定义,而是从对应的基表中产生(视图的数据来源). 示例脚本: CREATE TA ...

  8. 团队作业6——展示博客(Alpha版本)

    Deadline: 2017-5-7 22:00PM,以博客发表日期为准 评分基准: 按时交 - 有分,检查的项目包括后文的四个方面 团队成员介绍 Alpha阶段进展 团队合作,各成员分工 Beta阶 ...

  9. Swing-JTable检测单元格数据变更事件

    在JTable的初级教程中往往会提到,使用TableModel的 addTableModelListener方法可以监听单元格数据的变更,在其事件处理函,数tableChanged中,可以通过e.ge ...

  10. 201521123036 《Java程序设计》第3周学习总结

    本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来. 书面作业 Q1:代码阅读 public class Test1 { private ...