【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会通…

【FZU - 2039】Pets(二分图匹配,水题)

题干&#xff1a; 有n个人&#xff0c;m条狗&#xff0c;然后会给出有一些人不喜欢一些狗就不会购买&#xff0c;问最多能卖多少狗。。 Input There is a single integer T in the first line of the test data indicating that there are T(T≤100) test cases. In the fir…

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…

【BZOJ - 1305】dance跳舞(拆点网络流,建图,最大流,残留网络上跑最大流)

题干&#xff1a; 一次舞会有n个男孩和n个女孩。每首曲子开始时&#xff0c;所有男孩和女孩恰好配成n对跳交谊舞。每个男孩都不会和同一个女孩跳两首&#xff08;或更多&#xff09;舞曲。有一些男孩女孩相互喜欢&#xff0c;而其他相互不喜欢&#xff08;不会“单向喜欢”&am…

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

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

【HDU - 1530】Maximum Clique(最大团问题,图论)

题干&#xff1a; Given a graph G(V, E), a clique is a sub-graph g(v, e), so that for all vertex pairs v1, v2 in v, there exists an edge (v1, v2) in e. Maximum clique is the clique that has maximum number of vertex. Input Input contains multiple tests. …

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

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

【CF - 699C】 Vacations (日程安排 dp)

题干&#xff1a; Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day…

《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…

【2018山东省赛 - A】Anagram(贪心,费用流,KM算法)

题干&#xff1a; Problem Description Orz has two strings of the same length: A and B. Now she wants to transform A into an anagram of B (which means, a rearrangement of B) by changing some of its letters. The only operation the girl can make is to “incr…

【2018ACM山东省赛 - B】Bullet(二分 + 二分图匹配,匈牙利算法,卡常)

题干&#xff1a; Problem Description In GGO, a world dominated by gun and steel, players are fighting for the honor of being the strongest gunmen. Player Shino is a sniper, and her aimed shot kills one monster at a time. Now she is in an nnn \times nnn m…

13.Data Leakage

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

0.Overview----Machine Learning

本文为Kaggle Learn的Machine Learning课程的中文翻译&#xff0c;原文链接为&#xff1a;https://www.kaggle.com/learn/machine-learning 1.How Models Work The first step if youre new to machine learning 2.Explore Your Data Load data and set up your environment …

【2018ACM山东省赛 - C】Cities(最小生成树变形优化,贪心思维)

题干&#xff1a; Problem Description There are nnn cities in Byteland, and the ithi_{th}ith​ city has a value aia_iai​. The cost of building a bidirectional road between two cities is the sum of their values. Please calculate the minimum cost of connec…

项目总结1:微信扫码自动识别设备类型并跳转到相应的应用下载页面(apk或App Store)之解决方案

问题分析&#xff1a;普通页面一般无法调用微信的扫一扫接口&#xff0c;从而否定通过微信扫一扫功能给我们判断当前扫码的设备类型。 解决方案&#xff1a;通过应用下载页面自身来获取当前访问的客户端设备类型&#xff08;iPhone、Android、iPad&#xff09;&#xff0c;然后…

《TCP/IP详解》学习笔记(六):UDP 协议

UDP 简要介绍 UDP 是传输层协议&#xff0c;和 TCP 协议处于一个分层中&#xff0c;但是与 TCP 协议不同&#xff0c;UDP 协议并不提供超时重传&#xff0c;出错重传等功能&#xff0c;也就是说其是不可靠的协议。 UDP 协议头 1UDP 端口号 由于很多软件需要用到 UDP 协议&am…