【POJ - 2337】Catenyms(欧拉图相关,欧拉通路输出路径,tricks)

题干:

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.gophergopher.ratrat.tigeraloha.alohaarachnid.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
***

题目大意:

  给n个字符串,让你串成一个串,要求输出顺序,如果多解要求字典序最小

解题报告:

   不用并查集判连通,用dfs判连通就行了。注意路径记录的时候要先dfs再回溯记录,因为可能有这种情况。

所以你需要再倒回来路径的时候记录路径,因为首先欧拉通路他只有一个终点(再也走不动的地方),你最后走不动了回溯的时候着一定是倒着回溯的,最后倒着输出就行了。

再就是注意搜索的起点:

(1)如果发现所有节点的出度与入度都相等,那么有向图中存在欧拉回路,当然也一定存在欧拉通路了。这时以任一节点开始深搜一条路径即可。(因为字典序要求最小我们就从出现的字符集中最小的那个字母开始搜)
(2)如果发现存在 out[i] - in[i] == 1 的节点,那么欧拉通路是以out[i] - in[i] == 1的那个 i 为始点的,以 in[i] - out[i] == 1 的那个 i 为终点。这时我们要以这个out[i] - in[i] == 1 的节点为起点开始深搜。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<string,int> PSI;
const int MAX = 1000 + 5;
string s[MAX];
int n;
string ans[MAX];
int len[MAX],in[MAX],out[MAX],tot; 
bool vis[MAX];
vector<PSI> vv[129];
void dfs(char st) {int up = vv[st].size();for(int i = 0; i<up; i++) {PSI cur = vv[st][i];if(vis[cur.S]) continue;vis[cur.S] = 1;		dfs(cur.F[len[cur.S]-1]);		ans[++tot] = cur.F;}
}
int main()
{int t;cin>>t;while(t--) {tot=0;scanf("%d",&n);for(int i = 1; i<=127; i++) in[i]=out[i]=0,vv[i].clear();for(int i = 1; i<=n; i++) {vis[i] = 0;cin>>s[i];len[i] = s[i].length();}char minn = 'z';for(int i = 1; i<=n; i++) {			char st = s[i][0];char ed = s[i][len[i]-1];vv[st].pb(pm(s[i],i));in[ed]++;out[st]++;minn = min(minn,ed);minn = min(minn,st);}int flag = 1,ru=0,chu=0;for(int i = 'a'; i<='z'; i++) {sort(vv[i].begin(),vv[i].end());if(!in[i] && !out[i]) continue;if(in[i] == out[i]) continue;else if(in[i] - out[i] == 1) ru++;else if(out[i] - in[i] == 1) chu++,minn=i;else {flag = 0;break;}}if(flag == 0 || ru>1 || chu>1 || ru!=chu) puts("***");else {dfs(minn);if(tot != n) puts("***");else {for(int i = n; i>=1; i--) {if(i != n) printf(".");cout << ans[i];}puts("");}}} return 0 ;
}

总结:提问,如果要求输出欧拉回路的路径咋办?答:在起点那里稍微判断一下就行了。或者最后直接输出起点。因为既然是欧拉回路的话可以以任何一个点为起点,所以你钦点的起点最后一定可以绕回来,所以最后输出答案的时候先输出一遍起点就可以。或者在dfs的时候稍微记录一下起点。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440313.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

12.Cross-Validation

本教程是ML系列的一部分。 在此步骤中&#xff0c;您将学习如何使用交叉验证来更好地衡量模型性能。 What is Cross Validation 机器学习是一个迭代过程。 您将面临关于要使用的预测变量&#xff0c;要使用的模型类型&#xff0c;提供这些模型的参数等的选择。我们通过测量各…

服务器不显示u盘,服务器不读u盘启动

服务器不读u盘启动 内容精选换一换介绍使用Atlas 200 DK前需要准备的配件及开发服务器。Atlas 200 DK使用需要用户提前自购如表1所示配件。准备一个操作系统为Ubuntu X86架构的服务器&#xff0c;用途如下&#xff1a;为Atlas 200 DK制作SD卡启动盘。读卡器或者Atlas 200 DK会通…

Leetcode刷题实战(1):Two Sum

Leetcode不需要过多介绍了&#xff0c;今天一边开始刷题一边开始总结&#xff1a; 官网链接如下&#xff1a;https://leetcode.com/problemset/all/ 题1描述&#xff1a; 1Two Sum38.80%Easy Given an array of integers, return indices of the two numbers such that they…

信息服务器为什么选择在贵州,为啥云服务器在贵州

为啥云服务器在贵州 内容精选换一换当用户已在ECS服务购买GPU加速型云服务器&#xff0c;并且想在该云服务器上运行应用时&#xff0c;可以通过纳管的方式将该云服务器纳入VR云渲游平台管理。登录控制台&#xff0c;在服务列表中选择“计算 > VR云渲游平台”。在左侧导航栏&…

LeetCode刷题实战(2):Add Two Numbers

题2描述&#xff1a; 2Add Two Numbers29.10%Medium You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a…

《TCP/IP详解》学习笔记(四):ICMP 协议、ping 和 Traceroute

ICMP 协议介绍 前面讲到了&#xff0c;IP 协议并不是一个可靠的协议&#xff0c;它不保证数据被成功送达&#xff0c;那么自然的&#xff0c;保证数据送达的工作应该由其他的模块来完 成。其中一个重要的模块就是 ICMP(网络控制报文)协议。 当传送 IP 数据包发生错误--比如主机…

Apollo自动驾驶入门课程第①讲—无人驾驶概览

目录 1. 全面了解自动驾驶主要模块 2. 了解无人车的运作方式 3. 开放式软件栈 4. 本节其他重点 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 8月1日 在Apollo 3.0发布的同时&#xff0c;我们面向更多对自动驾驶感兴趣的开发…

《TCP/IP详解》学习笔记(五):IP选路、动态选路

静态 IP 选路 1一个简单的路由表 选路是 IP 层最重要的功能之一。前面的部分已经简单的讲过路由器是如何根据 IP 数据包的 IP 地址来选择路由的。 这里就不重复了。首先来看看一个简单的系统路由表&#xff1a; 对于一个给定的路由器,可以打印出五种不同的 flag&#xff1a; …

LeetCode刷题实战(43):Multiply Strings

题目描述&#xff1a; 43Multiply Strings28.7%Medium Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 "2", num2 "3" Output…

13.Data Leakage

本教程是ML系列的一部分。在此步骤中&#xff0c;你将学习什么是data leakage及如何预防它。 What is Data Leakage 数据泄漏是数据科学家需要理解的最重要问题之一。 如果您不知道如何防止它&#xff0c;则会频繁出现泄漏&#xff0c;并且会以最微妙和危险的方式破坏您的模…

Apollo自动驾驶入门课程第②讲 — 高精地图

目录 1. 高精地图与传统地图 2. 高精地图与定位、感知规划的关系 2.1 高精地图用于定位 2.2 高精地图用于感知 2.3 高精地图用于规划 3. Apollo高精度地图与构建 3.1 Apollo高精地图 3.2 Apollo高精地图的构建 本文转自微信公众号&#xff1a; Apollo开发者社区 原创&a…

项目总结2:ionic3开发跨平台App如何设置和替换应用图标及启动图

前言&#xff1a; 和原生开发一样&#xff0c;ionic官方提供的设置方式也很简单&#xff0c;只不过多了一个步骤&#xff1a;基于ionic命令的方式自动修改全局的配置文件config.xml。 设置或替换应用图标和应用启动图&#xff1a; 把UI提供的图标拿过来改成特定的名称"i…

LeetCode刷题实战(13):Roman to Integer

题目描述&#xff1a; 13 Roman to Integer 49.5%Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D …

1.Intro to Deep Learning and Computer Vision

Intro 这是Kaggle深度学习教育课程的第一课。 在本课程结束后&#xff0c;您将了解卷积。 卷积是计算机视觉&#xff08;以及许多其他应用程序&#xff09;中深度学习模型的基本构建块。 之后&#xff0c;我们将很快开始使用世界一流的深度学习模型。 Lesson [1] from IPy…

SpringMVC常用的视图接口分类及实现类

SpringMVC中常用的视图接口分类及对应的实现类&#xff1a; URL资源视图&#xff1a;InternalResourceView、JstlView 文档视图&#xff1a;AbstractExcelView、AbstractPdfView 报表视图&#xff1a;ConfigurableJsperReportsView等JasperReports报表技术的视图 JSON视图&…

Apollo自动驾驶入门课程第③讲 — 定位

目录 1. 定位的概述 2. 定位方法介绍 2.1 GNSS RTK 2.2 惯性导航 2.3 激光雷达定位 2.4 视觉定位 2.5 Apollo定位 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 8月17日 上周我们发布了 Apollo入门课堂第②讲—高精地图&…

Linux与Bash 编程——Linux文件处理命令-L1

目录&#xff1a; linux系统与shell环境准备 Linux系统简介操作系统简史Linux的发行版&#xff1a;Linux与Windows比较&#xff1a;Linux安装安装包下载Linux的访问方式远程登录方式远程登录软件&#xff1a;mobaxterm的使用&#xff1a;使用电脑命令行连接&#xff1a;sshd的…

网络编程懒人入门(一):快速理解网络通信协议(上篇)

转自即时通讯网&#xff1a;http://www.52im.net/ 原作者&#xff1a;阮一峰(ruanyifeng.com&#xff09;&#xff0c;本文由即时通讯网重新整理发布&#xff0c;感谢原作者的无私分享。 1、写在前面 论坛和群里常会有技术同行打算自已开发IM或者消息推送系统&#xff0c;很…

PCA算法中样本方差和协方差的无偏估计与n-1的由来

原文出处&#xff1a; http://blog.sina.com.cn/s/blog_c96053d60101n24f.html 在PCA算法中的方差协方差计算公式中除数为什么是n-1? 假设X为独立同分布的一组随机变量&#xff0c;总体为M&#xff0c;随机抽取N个随机变量构成一个样本&#xff0c;和是总体的均值和方差, 是常…

Apollo自动驾驶入门课程第④讲 — 感知(上)

目录 1. 感知的概述 2. 计算机视觉 3. 摄像头图像 4. LiDAR图像 5. 机器学习 6. 神经网络 7. 反向滤波法 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 8月23日 上一篇文章中&#xff0c;我们发布了无人驾驶技术的 定位篇…