POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)
题目链接>>>>>>
题目大意:
给出一些字符串,问能否将这些字符串 按照 词语接龙,首尾相接 的规则 使得每个字符串出现一次
如果可以 按字典序输出这个字符串序列
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#define M 1050
using namespace std;
int n, top;
struct edge {
int to, vis, id; //to代表边的终点,id代表边的编号
};
vector<edge>w[M];
string str[M]; //原来还可以这样定义字符串数组
int ans[M];
int fa[]; int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
} void fleury(int loc)
{
for (int i = ; i<w[loc].size(); i++)
if (!w[loc][i].vis)
{
w[loc][i].vis = ;
fleury(w[loc][i].to);
ans[top++] = w[loc][i].id;
}
return;
} int main()
{
int indegree[], outdegree[];
int T;
scanf("%d", &T);
while (T--)
{
top = ;
for (int i = ; i<; i++)
{
w[i].clear();
outdegree[i] = indegree[i] = ;
fa[i] = i;
}
scanf("%d", &n);
int a, b;
for (int i = ; i<n; i++)
cin >> str[i];
sort(str, str + n); //根据字典序排序
edge edg;
int start;
for (int i = ; i<n; i++)
{
a = str[i][] - 'a';
b = str[i][str[i].size() - ] - 'a';
indegree[a]++;
outdegree[b]++;
fa[find(a)] = find(b);
edg.to = b; edg.vis = ; edg.id = i;
w[a].push_back(edg);
if (i == ) //重要 起点必须为初始化为第一条边的出节点(字典序最小,且满足 欧拉回路 的的要求)
start = a;
} int ss = , num = , start_num = , end_num = ;
for (int i = ; i<; i++)
{
if ((indegree[i] || outdegree[i]) && find(i) == i) //find(i)==i是用来判断整个图是否连通的,因为图存在欧拉通路的条件之一就是必须是连通图
ss++; //结合下面的ss==1,来理解,因为如果整个图是连通的,ss就只会在当i为根节点的时候+1
if (indegree[i] != outdegree[i])
{
if (outdegree[i] - indegree[i] == -)
start = i, start_num++; //这里和上面的初始化start的步骤不懂,做题的时候就是卡在了选取第一个出节点的步骤
else if (outdegree[i] - indegree[i] == )
end_num++;
num++;
}
}
if ((num == || (num == && start_num == && end_num == )) && ss == ) //存在欧拉通路的条件
{
fleury(start); //我对start的选取不是很理解
for (int i = top - ; i >= ; i--) //要使输出的单词按字典序输出
{
if (i == )
cout << str[ans[i]] << endl;
else
cout << str[ans[i]] << ".";
}
}
else
cout << "***" << endl;
}
return ;
}
2018-04-07
POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)的更多相关文章
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...
- POJ 2337 Catenyms (欧拉图)
本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...
- POJ 2337 Catenyms
http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...
- POJ 2337 Catenyms (欧拉回路)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8173 Accepted: 2149 Descript ...
- POJ 2337 Catenyms(有向图的欧拉通路)
题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...
- Poj 2337 Catenyms(有向图DFS求欧拉通路)
题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...
- poj 2337 有向图输出欧拉路径
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10186 Accepted: 2650 Descrip ...
随机推荐
- jQuery - ajaxUpLoad.js
ajaxFileUpload是一个异步上传文件的jQuery插件 语法:$.ajaxFileUpload([options]) options参数说明: 参数 作用 url 上传处理程序地址 file ...
- 【centos】 error: command 'gcc' failed with exit status 1
原文连接http://blog.csdn.net/fenglifeng1987/article/details/38057193 用安装Python模块出现error: command 'gcc' f ...
- 三 、 Multivariance Linear Regssion练习(转载)
转载:http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2962116.html 前言: 本文主要是来练习多变量线性回归问题(其实本文也就3个 ...
- windows环境用python修改环境变量的注意点(含代码)
1.部分环境变量字段需要保留原来的值,只是做添加,不可以替换 2.Path和PATH对于python来说是一样的,也就是说存在名为Path的环境变量时,添加PATH的环境变量,会覆盖原有的Path环境 ...
- nagios系列(四)之nagios主动方式监控tcp常用的80/3306等端口监控web/syncd/mysql及url服务
nagios主动方式监控tcp服务web/syncd/mysql及url cd /usr/local/nagios/libexec/ [root@node4 libexec]# ./check_tcp ...
- WM8960音频播放
第一节 音频播放原理首先需要申明一下,本章的代码来自网络,参考了亚嵌教育李明老师(论坛ID:limingth)的帖子: http://www.arm9home.net/read.php?tid=205 ...
- 服务器上安装FileZilla Server连接时报You appear to be behind a NAT router. Please configure the passive mode settings and forward a range of ports in your router.
官方资源下载链接:客户端,或者直接点击下载“FileZilla_3.24.0_win64-setup.exe”:服务端,或者直接点击下载“FileZilla_Server-0_9_60_2.exe”: ...
- Ex 6_20 最优二叉搜索树..._第六次作业
假设关键字的总数为n,用c[i,j]表示第i个关键字到第j个关键字的最优二叉查找树的代价,我们的目标是求c[0,n-1].要求c[i,j],首先要从第i个关键字到第j个关键字中选一个出来作为根结点,选 ...
- 一台电脑,两个及多个git账号配置
1. 生成两[三]个ssh公钥私钥 方法参照:http://www.cnblogs.com/fanbi/p/7772812.html第三步骤 假定其中一个是id_rsa, 另一个时id_rsa_two ...
- spring mvc 校验@NULL
一需要的包 1 validation-api-1.0.0.GA.jar:JDK的接口: 2 hibernate-validator-4.2.0.Final.jar 是对上述接口的实现: 二 若在pom ...