目录

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. [BZOJ4651][NOI2016]网格(Tarjan)

    下面直接给出结论,相关证明见官方题解. 1.若跳蚤数不超过1或仅有两只跳蚤且相邻,则答案为-1. 2.若跳蚤形成的连通块个数大于1,则答案为0. 3.若跳蚤之间建图存在割点,则答案为1. 4.否则为2 ...

  2. 【20181026T2】**图【最小瓶颈路+非旋Treap+启发式合并】

    题面 [错解] 最大最小?最小生成树嘛 蛤?还要求和? 点分治? 不可做啊 写了个MST+暴力LCA,30pts,140多行 事后发现30分是给dijkstra的 woc [正解] 树上计数问题:①并 ...

  3. arraylist-lambada-性能测试

    package cn.com.one;import java.util.ArrayList;public class ttt { public static void main(String [] a ...

  4. April Fools Day Contest 2016 B. Scrambled

    B. Scrambled 题目连接: http://www.codeforces.com/contest/656/problem/B Description Btoh yuo adn yuor roo ...

  5. VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) D. Running with Obstacles 贪心

    D. Running with Obstacles 题目连接: http://www.codeforces.com/contest/637/problem/D Description A sports ...

  6. debian禁止或者允许指定ip访问远程mysql、ssh、rsynccat /etc/xinetd.conf

    如果没有安装xinetd,安装xinetd apt-get install xinetd 然后创建配置文件 vi /etc/xinetd.d/mysqld service login { socket ...

  7. layoutit note

    Head: JavaScript -> Navbar Menu: JavaScript  -> Collapse Compnents -> Panels Compnents -> ...

  8. drbd + pacemaker

    Centos6.5 X86 64 2014-04-27 # yum -y install corosync pacemaker # -.el6.x86_64.rpm pssh--.el6.x86_64 ...

  9. 微信小程序导航栏,下面内容滑动,上册导航栏跟着滑动,内容随着导航栏滑动

    16.类似微信导航栏滑动.png 今日头条导航栏,下面滑动上面跟着滑动 index.wxml <swiper class="content" style="heig ...

  10. iOS学习之sqlite的创建数据库,表,插入查看数据

    目录(?)[-] 新建项目sqliteDemo添加使用sqlite的库libsqlite3dylib sqlite 的方法 获取沙盒目录并创建或打开数据库 创建数据表 插入数据 查询数据库并打印数据 ...