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 ...
随机推荐
- Sqoop异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject
18/12/07 01:09:03 INFO mapreduce.ImportJobBase: Beginning import of staffException in thread "m ...
- Python 入门基础17 --加密、表格、xml模块
今日内容: 1.hashlib模块:加密 2.hmac模块:加密 3.configparser模块:操作配置文件 4.subprocess模块:操作shell命令 5.xlrd模块:excel 6.x ...
- mac系统在配置navicat时连接数据的时候提示can't connect to mysql server on '127.0.0.1'
新建数据库连接的时候,将默认的端口号更改掉,改为3307,即可解决这个问题. 具体是为什么我也不清楚,我自己想的一个可能就是mac电脑 上的某个程序可能已经占用了3306那个默认的端口,因 ...
- SpringMVC参数绑定(四)
1.默认支持的参数类型 处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值. HttpServletRequest 通过request对象获取请求信息 HttpServletResponse ...
- Pytorch 资料汇总(持续更新)
1. Pytorch 论坛/网站 PyTorch 中文网 python优先的深度学习框架 Pytorch中文文档 Pythrch-CN文档地址 PyTorch 基礎篇 2. Pytorch 书籍 深度 ...
- Websphere MQ Cluster
大纲: 1.什么是集群 2.建立一个基本的集群 3.DISPLAY命令 4.负载均衡 5.高级配置和管理 6.答疑 7.关于文章.红宝书等 一. 什么是集群 集群就是Websphere M ...
- 『转载』hadoop2.x常用端口、定义方法及默认端口
『转载』hadoop2.x常用端口.定义方法及默认端口 1.问题导读 DataNode的http服务的端口.ipc服务的端口分别是哪个? NameNode的http服务的端口.ipc服务的端口分别是哪 ...
- oracle instantclient_11_2 配置文件tnsnames.ora
文件所在位置(不同版本位置可能不同): oracle\product\10.2.0\client_1\NETWORK\ADMIN\tnsnames.ora WDDB = (DESCRIPTION = ...
- IDEA测试结果查看
点击漏斗图标切换查看测试日志信息,点击,导出测试报告
- bootgrid 刷新保持当前排序
1. 前言 主要是利用了HTHNL5的localStorage技术和用ajax传输一个数组到后台并进行判断.这篇文章是解决一个小需求而来的,主要是用来记录. 2. 代码 JavaScript: var ...