Catenyms POJ - 2337(单词+字典序输出路径)
题意:
就是给出几个单词 看能否组成欧拉回路或路径 当然还是让输出组成的最小字典序的路
解析:
还是把首尾字母看成点 把单词看成边 记录边就好了
这题让我对fleury输出最小字典序又加深了一些认识
fleury输出最小字典序 就必须保证对应输出的边或点 按从小到大的顺序去走
所以我们先保存 然后排序 然后从大到小加边
因为我们用的是邻接表 邻接表是从当前起点u的最后一个加入的边 开始的 。。所以我们要对应起来
把边从大到小依次加入
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int in[maxn], out[maxn], f[maxn];
int n, s, cnt, tot;
int head[maxn], stk[maxn], vis[maxn];
string str[maxn];
struct node
{
int u, v, next, flag, id;
}Node[maxn]; void add(int u, int v, int id)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].id = id;
Node[cnt].flag = ;
Node[cnt].next = head[u];
head[u] = cnt++;
} void dfs(int u)
{
for(int i = head[u]; i != -; i = Node[i].next)
{
if(!Node[i].flag)
{
Node[i].flag = ;
dfs(Node[i].v);
stk[tot++] = Node[i].id;
}
} } void print()
{
int sh = ;
for(int i = tot - ; i >= ; i--)
{
if(sh++) printf(".");
cout << str[stk[i]];
}
printf("\n");
} int find(int x)
{
return f[x] == x ? x : (f[x] = find(f[x]));
} void init()
{
mem(in, );
mem(out, );
mem(vis, );
mem(head, -);
cnt = tot = ;
for(int i = ; i < maxn; i++) f[i] = i;
} int main()
{
int T;
rd(T);
while(T--)
{
init();
rd(n);
for(int i = ; i <= n; i++)
cin >> str[i];
sort(str + , str + n + );
for(int i = n; i >= ; i--)
{
int u = str[i][] - 'a' + ; int v = str[i][str[i].size() - ] - 'a' + ;
vis[u] = vis[v] = ;
add(u, v, i);
in[v]++, out[u]++;
int l = find(u), r = find(v);
if(l != r) f[l] = r;
}
int cnt1 = , cnt2 = , cnt3 = , flag = ;
int x = find(str[][] - 'a' + );
s = str[][] - 'a' + ;
for(int i = ; i <= ; i++)
{
if(!vis[i]) continue;
if(find(i) != x) flag = ;
if(in[i] != out[i]) cnt3++;
if(in[i] + == out[i]) cnt1++, s = i;
if(out[i] + == in[i]) cnt2++;
}
if(!flag && (cnt3 == || cnt3 == && cnt1 == && cnt2 == ))
{
dfs(s);
print();
}
else
printf("***\n"); } return ;
}
Catenyms POJ - 2337(单词+字典序输出路径)的更多相关文章
- hdu Minimum Transport Cost(按字典序输出路径)
http://acm.hdu.edu.cn/showproblem.php? pid=1385 求最短路.要求输出字典序最小的路径. spfa:拿一个pre[]记录前驱,不同的是在松弛的时候.要考虑和 ...
- POJ 2337 【字典序】【欧拉回路】
题意: 给你一些单词,判断这些单词能否在保证首尾单词相同的情况下连成一排. 如果有多组解,输出字典序最小的一组解. 这题... WA了两天. 错误有以下: 1.没有初始化好起始位置,默认起始位置是a了 ...
- 迷宫问题 POJ - 3984 (搜索输出路径)
题目大意 题目不需要大意,poj居然还有中文题 鸣谢 特别鸣谢ljc大佬提供的方法!!! 解法 我们可能输出个最短路径的长度比较简单,但是输出最短路径真的是没有做过,这里有一种简单的方法 因为我们的d ...
- Day 4 -E - Catenyms POJ - 2337
A catenym is a pair of words separated by a period such that the last letter of the first word is th ...
- poj 3414 Pots(bfs+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- HD1385Minimum Transport Cost(Floyd + 输出路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)
题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串 按照 词语接龙,首尾相接 的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...
- POJ 2337 Catenyms
http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...
随机推荐
- C语言中getch()、getche()和getchar()
本文章为转载文章,文档贡献者wdzhangxiang 个人网址:www.baidu.com/p/wdzhangxiang 首先不要忘了,要用getch()必须引入头文件conio.h,以前学C语言的时 ...
- odoo学习之弹框显示
按条件隐藏: <xpath expr="//group[1]" position="attributes"> <attribute name= ...
- CMD命令查看当前电脑安装所有版本.NET Core SDK(转载)
dotnet --version 查看当前使用版本 dotnet --info 安装的所有版本 包括版本地址 也可用命令帮助 dotnet help 原文链接
- 如何在Virtual box 下安装Mac os
这几天,突然奇想,想要试一试Mac os ,毕竟是贵族系统,装完之后,确实感觉字体很不错. 其他更优秀的功能还没发现,不过,还是希望在这里做一个记录. 以下附录我参照的网址:https://blog ...
- npm install报错 npm ERR! enoent ENOENT: no such file or directory
在npm之后出现如下错误: $ npm install npm WARN checkPermissions Missing write access to /Users/lucas/code/js/v ...
- [Codeforces1137D]Cooperative Game
[Codeforces1137D]Cooperative Game Tags:题解 题意 这是一道交互题. 给你一张下面这样的地图,由一条长为\(t\)的有向链和一个长为\(c\)的环构成. 现在你有 ...
- sql语句——行列互换
SELECT 年份, SUM(case when 季度=1 then 销量 else 0 end) as 一季度, SUM(case when 季度=2 then 销量 else 0 end) as ...
- zifutongji
第三次作业要求我们自己写程序,我算我们班写的比较晚的了,我听他们写的都是在文件中写一段代码,然后读出来.我们班大部分都是,所以,我就想可不可以跟他们不一样呢,写一个属于自己的思路. 所以我想到了数组. ...
- 13.14.15.16.17&《一个程序猿的生命周期》读后感
13.TDS 的标准是什么,怎么样才能认为他是一个标准的TDS?? 14.软件的质量包括哪些方面,如何权衡软件的质量? 15.如何解决功能与时间的矛盾,优秀的软件团队会发布有已知缺陷的软件么? 16. ...
- python中的文件读写(open()函数、with open('file_directory','r') as f:、read()函数等)
python中也有文件读写,通过调用内置的读写函数.可以完成文件的打开/关闭.读.写入.追加等功能. open()函数 open()函数为python中的打开文件函数,使用方式为: f = open( ...