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


中文说明

类义词是由句号分隔的一对词,第一个词的最后一个字母与第二个词的最后一个字母相同。例如,以下是类别名称:

狗。地鼠

地鼠

老鼠,老虎

阿罗哈,阿罗哈

蛛形纲动物

复合词义是由三个或三个以上的单词组成的序列,这些单词之间用句点隔开,从而使每对相邻的单词形成一个词义。例如,

阿罗哈。阿罗哈。蛛形纲动物。狗。地鼠。老虎。

给定一个小写单词的字典,您将找到一个复合的catenym,它只包含每个单词一次。

输入

标准输入的第一行包含t,即测试用例的数量。每个测试用例以3<=n<=1000-字典中的单词数开始。后面有n个不同的字典单词;每个单词本身就是一行上1到20个小写字母之间的字符串。

输出

对于每一个测试用例,输出一行,给出字典式最小复合类别名,该类别名恰好包含每个字典单词一次。如果没有解决方案,则输出“***”。

package com.liuzhen.practice;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner; public class Main {
public static int MAX = 30; //英文字母共26个
@SuppressWarnings("unchecked")
public static ArrayList<edge>[] map = new ArrayList[MAX];
public static String[] path;
public static int count; //统计DFS遍历访问的边数目,即检测图的连通性
public static int start;
public static int[] inDegree = new int[MAX];
public static int[] outDegree = new int[MAX];
public static ArrayList<String> result = new ArrayList<String>(); class MyComparator implements Comparator<edge> { public int compare(edge arg0, edge arg1) {
String A = arg0.word;
String B = arg1.word;
int judge = A.compareTo(B);
if(judge > 0)
return 1;
else if(judge < 0)
return -1;
return 0;
} } static class edge {
public int a; //单词的第一个字母序号
public int b; //单词最后一个字母序号
public String word; //具体单词
public boolean used; //判断单词是否被访问 public edge(int a, int b, String word) {
this.a = a;
this.b = b;
this.word = word;
used = false;
}
} public void init(int k) {
start = MAX;
for(int i = 0;i < MAX;i++) {
map[i] = new ArrayList<edge>();
inDegree[i] = 0;
outDegree[i] = 0;
}
path = new String[k];
for(int i = 0;i < k;i++)
path[i] = "";
count = 0;
} public boolean judgeDegree() {
int in = 0, out = 0;
for(int i = 1;i < map.length;i++) { //对map[i]中单词进行字典序排序
if(map[i].size() > 1)
Collections.sort(map[i], new MyComparator());
} for(int i = 0;i < inDegree.length;i++) {
if(inDegree[i] == outDegree[i])
continue;
else if(inDegree[i] - outDegree[i] == 1)
in++;
else if(outDegree[i] - inDegree[i] == 1) {
out++;
start = i; //此时,可能存在欧拉路径,必须从入度小于出度的点开始遍历
} else
return false;
}
if(in == out && (in == 0 || in == 1))
return true;
return false;
} public void dfs(int begin) {
for(int i = 0;i < map[begin].size();i++) {
edge temp = map[begin].get(i);
if(temp.used == false) {
temp.used = true;
path[count++] = temp.word;
dfs(temp.b);
}
}
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t > 0) {
t--;
int k = in.nextInt();
test.init(k);
for(int i = 0;i < k;i++) {
String A = in.next();
int a = A.charAt(0) - 'a';
int b = A.charAt(A.length() - 1) - 'a';
start = Math.min(start, Math.min(a, b));
map[a].add(new edge(a, b, A));
outDegree[a]++;
inDegree[b]++;
}
StringBuilder temp = new StringBuilder("");
if(test.judgeDegree()) { //满足欧拉回路或者欧拉路径对顶点度的要求
test.dfs(start);
if(count == k) { //图连通
for(int i = 0;i < k;i++) {
temp.append(path[i]);
if(i != k - 1)
temp.append(".");
}
} else {
temp.append("***");
}
} else {
temp.append("***");
}
result.add(temp.toString());
}
for(int i = 0;i < result.size();i++)
System.out.println(result.get(i));
}
}

运行结果:

6
aloha
arachnid
dog
gopher
rat
tiger
oak
maple
elm
aloha.arachnid.dog.gopher.rat.tiger
***

Java实现Catenyms(并查集+dfs+欧拉回路)的更多相关文章

  1. hdu 1116 并查集判断欧拉回路通路

    判断一些字符串能首尾相连连在一起 并查集求欧拉回路和通路 Sample Input 3 2 acm ibm 3 acm malform mouse 2 ok ok Sample Output The ...

  2. hdu6370 并查集+dfs

    Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. HDU-1878 欧拉回路(并查集,欧拉回路性质)

    欧拉回路 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  6. POJ1291-并查集/dfs

    并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...

  7. F2 - Spanning Tree with One Fixed Degree - 并查集+DFS

    这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...

  8. UVA208-Firetruck(并查集+dfs)

    Problem UVA208-Firetruck Accept:1733  Submit:14538 Time Limit: 3000 mSec  Problem Description The Ce ...

  9. hdu1878-并查集,欧拉回路

    纯裸题..写着方便理解... 题意:判断一个无向图是否存在欧拉回路... 解题思路:并查集判断一下是否联通,然后再判断一下点的度数是否为偶数就行了: #include<iostream> ...

随机推荐

  1. 面试总结:鹅厂Linux后台开发面试笔试C++知识点参考笔记

    文章每周持续更新,各位的「三连」是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) 文章是由自己笔试面试腾讯的笔记整理而来,整理的时候又回顾了一遍,中间工 ...

  2. 重要的serialVersionUID

    所有序列化的DO都需要加上 serialVersionUID 否则未来可能就有一个坑在等着你 当你需要修改序列化的实体累的时候 之前缓存内容反序列化就会失败,如果这个缓存很多个地方都在存取 使用 那么 ...

  3. windows定时任务schtasks命令详细解

    SCHTASKS /Create [/S system [/U username [/P [password]]]] [/RU username [/RP password]] /SC schedul ...

  4. slf4j、log4j、 logback关系详解和相关用法

    slf4j log4j logback关系详解和相关用法 写java也有一段时间了,一直都有用slf4j log4j输出日志的习惯.但是始终都是抱着“拿来主义”的态度,复制粘贴下配置文件就开始编码了, ...

  5. MySQL(8)— 权限管理和备份

    8-1.用户管理 SQL yog 可视化进行管理 sql -- 创建用户:CREATE USER 用户名 IDENTIFIED BY '密码' CREATE USER feng IDENTIFIED ...

  6. Hyperledger Fabric——balance transfer(四)安装和实例化chaincode

    详细解析blance transfer示例的安装(install)和实例化(Instantiate)链码(chaincode)的过程.安装chaincode会根据本地的链码文件生成chaincode镜 ...

  7. Android gradle 自定义插件

    Gradle 的插件有三种打包方式: 构建脚本:插件逻辑写在 build.gradle 中,适用于逻辑简单的任务,但是该方式实现的插件在该构建脚本之外是不可见的,只能用于当前脚本. buildSrc项 ...

  8. 【常用工具】vagrant的box哪里下?镜像在哪儿找?教你在vagrant官网下载各种最新.box资源

    进入vagrant官网 : https://www.vagrantup.com/ 点击findbox[寻找box],进入有很多box的列表 : https://app.vagrantup.com/bo ...

  9. hdu2093 考试排名(还需完善)

    下面代码是借鉴的.好多的知识点等着完善 #include <iostream> #include <string> #include <algorithm> usi ...

  10. vue移动端转场动画

    vue移动端转场动画 1.介绍:使用vue移动端做项目的时候,为了用户的体验良好,我们需要页面有一种进入和转出的效果 // 在App.vue根组件中 <template> <div ...