目录

1 问题描述

2 解决方案

 


1 问题描述

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
***

Source


2 解决方案

具体代码如下:

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));
}
}

运行结果:

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

参考资料:

1. 欧拉回路

算法笔记_148:有向图欧拉回路求解(Java)的更多相关文章

  1. 算法笔记_147:有向图欧拉回路判断应用(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 Description In order to make their sons brave, Jiajia and Wind take them t ...

  2. 算法笔记_023:拓扑排序(Java)

    目录 1 问题描述 2 解决方案 2.1 基于减治法实现 2.2 基于深度优先查找实现 1 问题描述 给定一个有向图,求取此图的拓扑排序序列. 那么,何为拓扑排序? 定义:将有向图中的顶点以线性方式进 ...

  3. 算法笔记_142:无向图的欧拉回路求解(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8 ...

  4. 算法笔记_144:有向图强连通分量的Tarjan算法(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...

  5. 算法笔记_132:最大流量问题(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 何为最大流量问题? 给定一个有向图,并为每一个顶点设定编号为0~n,现在求取从顶点0(PS:也可以称为源点)到顶点n(PS:也可以称为汇点)后,顶点 ...

  6. 算法笔记_228:信用卡号校验(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么担心,因为并不是一个随便的信用卡号码都是合法的,它必须通过Luhn算法来验证 ...

  7. 算法笔记_138:稳定婚姻问题(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 何为稳定婚姻问题? 有一个男士的集合Y = {m1,m2,m3...,mn}和一个女士的计划X = {n1,n2,n3,...,nn}.每一个男士有 ...

  8. 算法笔记_137:二分图的最大匹配(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 何为二分图的最大匹配问题? 引用自百度百科: 首先得说明一下何为匹配: 给定一个二分图G,在G的一个子图M中,M的边集{E}中的任意两条边都不依附于 ...

  9. 算法笔记_042:求最小公倍数(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 何为最小公倍数?能同时被数字m和数字n整除的最小整数.例如,24和60的最小公倍数等于120.下面请编写相关函数实现求取数字m和n的最小公倍数. 2 ...

随机推荐

  1. CUDA学习笔记4:CUDA(英伟达显卡统一计算架构)代码运行时间测试

    CUDA内核运行时间的测量函数 cudaEvent_t start1; cudaEventCreate(&start1); cudaEvent_t stop1; cudaEventCreate ...

  2. YanghuiTriangle

    Demand 1 用实现循环队列 2 参考PPT用循环队列打印杨辉三角 3 用JDB或IDEA单步跟踪排队情况,画出队列变化图,包含自己的学号信息 4 把代码推送到代码托管平台 5 把完成过程写一篇博 ...

  3. 1 Spring4 之环境搭建和HelloWorld

    1 Spring 是什么? 具体描述 Spring: 轻量级:Spring 是非侵入性的- 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API 依赖注入(DI --- dep ...

  4. Mybatis 删除多条数据XML SQL语句删除

    Mybatis 删除多条数据XML SQL语句删除 1.删除多条数据SQL写法 <delete id="deleteParamsByIds"> delete from ...

  5. 区间DP POJ 1141 Brackets Sequence

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29520   Accepted: 840 ...

  6. POJ 2482 Stars in Your Window 线段树

    如果按一般的思路来想,去求窗户能框住的星星,就很难想出来. 如果换一个思路,找出每颗星星能被哪些窗户框住,这题就变得非常简单了. 不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其 ...

  7. php uncode 转汉字编码

    $test = "%u4E0A%u6D77%u9EC4%u6D66";//$test = '\u5e86\u91cd\u5e86'; //庆重庆$temp = explode('% ...

  8. Python3的变化

    http://www.cnblogs.com/tips4python/archive/2011/05/31/2064290.html print 由一个语句(statement)变为一个函数 Pyth ...

  9. 给WebAPI的REST接口服务添加测试页面(一)

    当使用WebAPI提供REST服务的时候,一个经常进行的操作是对接口进行测试.Asp.net WebAPI框架本身并没有提供这一接口,不过由于提供的是标准的REST服务,是可以非常方便的使用一些第三方 ...

  10. Marvell w8782 sdio wifi AP模式设置

    http://blog.csdn.net/junllee/article/details/8895908 w8782工作在station模式一切正常,于是想试试AP模式(master mode): A ...