算法笔记_148:有向图欧拉回路求解(Java)
目录
1 问题描述
Description
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
Output
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)的更多相关文章
- 算法笔记_147:有向图欧拉回路判断应用(Java)
目录 1 问题描述 2 解决方案 1 问题描述 Description In order to make their sons brave, Jiajia and Wind take them t ...
- 算法笔记_023:拓扑排序(Java)
目录 1 问题描述 2 解决方案 2.1 基于减治法实现 2.2 基于深度优先查找实现 1 问题描述 给定一个有向图,求取此图的拓扑排序序列. 那么,何为拓扑排序? 定义:将有向图中的顶点以线性方式进 ...
- 算法笔记_142:无向图的欧拉回路求解(Java)
目录 1 问题描述 2 解决方案 1 问题描述 John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8 ...
- 算法笔记_144:有向图强连通分量的Tarjan算法(Java)
目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...
- 算法笔记_132:最大流量问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 何为最大流量问题? 给定一个有向图,并为每一个顶点设定编号为0~n,现在求取从顶点0(PS:也可以称为源点)到顶点n(PS:也可以称为汇点)后,顶点 ...
- 算法笔记_228:信用卡号校验(Java)
目录 1 问题描述 2 解决方案 1 问题描述 当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么担心,因为并不是一个随便的信用卡号码都是合法的,它必须通过Luhn算法来验证 ...
- 算法笔记_138:稳定婚姻问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 何为稳定婚姻问题? 有一个男士的集合Y = {m1,m2,m3...,mn}和一个女士的计划X = {n1,n2,n3,...,nn}.每一个男士有 ...
- 算法笔记_137:二分图的最大匹配(Java)
目录 1 问题描述 2 解决方案 1 问题描述 何为二分图的最大匹配问题? 引用自百度百科: 首先得说明一下何为匹配: 给定一个二分图G,在G的一个子图M中,M的边集{E}中的任意两条边都不依附于 ...
- 算法笔记_042:求最小公倍数(Java)
目录 1 问题描述 2 解决方案 1 问题描述 何为最小公倍数?能同时被数字m和数字n整除的最小整数.例如,24和60的最小公倍数等于120.下面请编写相关函数实现求取数字m和n的最小公倍数. 2 ...
随机推荐
- 范浩强treap——可持久化
当平衡树需要可持久化的时候,意味着我们需要访问以前的某个时间点的平衡树,就要保持以前的树形态不变,新建一个时间戳,构建一棵新的树. 如果用以前的旋转treap可能就不方便做到(又要打时间戳,又要新建节 ...
- LOJ#2471「九省联考 2018」一双木棋 MinMax博弈+记搜
题面 戳这里 题解 因为每行取的数的个数是单调不增的,感觉状态数不会很多? 怒而记搜,结果过了... #include<bits/stdc++.h> #define For(i,x,y) ...
- bzoj 3969: [WF2013]Low Power 二分
3969: [WF2013]Low Power Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
- Ubuntu 16.09开启iptables的日志实现调试
1.先配置日志文件输出 参考:http://www.cnblogs.com/EasonJim/p/8413535.html 2.配置日志打点 参考:http://www.cnblogs.com/Eas ...
- Django 中文显示
Django中文显示: 1.如要在.py文件中显示中文,在文件首行加上:# -*- coding: utf-8 -*- 2.如要在html文件中显示中文,要将文件保存为UTF-8格式 3.在setti ...
- 源码分析:动态分析 Linux 内核函数调用关系
源码分析:动态分析 Linux 内核函数调用关系 时间 2015-04-22 23:56:07 泰晓科技 原文 http://www.tinylab.org/source-code-analysi ...
- 三分钟教你学Git (四)之紧急救助
假设你不小心git reset --hard HEAD^ 然后这个commit又没有在别的git仓库中,怎么办?是不是这次改动就丢了呢? 当然不是,git为我们每次都历史都保留了reference l ...
- winform 取消datagridview第一行选中状态
C# WinForm 取消DataGridView的默认选中Cell 使其不反蓝 http://www.cnblogs.com/freeliver54/archive/2009/02/16/13913 ...
- 后端程序员写的前端js代码模板
看几天的javascript面向对象和基础等之类相关javascript的知识,因为自己是写php的,也写过java,所以想在写javascript代码的时候也能用上面向对象的思想, 折腾了一整天的j ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...