poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337
题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接。问能否够每一个单词用一次,将全部单词连接,能够则输出字典序最小的序列。
代码:
(bin 神的板子)
#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>using namespace std;int t;
int n;
string s[1010];struct Edge
{int to, next;int index;bool flag;
}edge[2010];int head[300], tot;void init()
{tot = 0;memset(head,-1,sizeof(head));
}void addedge(int u, int v, int index)
{edge[tot].to = v;edge[tot].next = head[u];edge[tot].index = index;edge[tot].flag = false;head[u] = tot++;
}int in[250], out[250];int cnt;
int ans[1010];void dfs(int u)
{for (int i = head[u]; i != -1; i = edge[i].next){if (!edge[i].flag){edge[i].flag = true;dfs(edge[i].to);ans[cnt++] = edge[i].index;}}
}int main()
{int t;cin >> t;while (t--){scanf("%d",&n);for (int i = 0; i < n; i++)cin >> s[i];sort(s, s + n);init();memset(in,0,sizeof(in));memset(out, 0, sizeof(out));int start = 100;for (int i = n - 1; i >= 0; i--){int u = s[i][0] - 'a';int v = s[i][s[i].length() - 1] - 'a';addedge(u,v,i);out[u]++;in[v]++;if (u < start) start = u;if (v < start) start = v;}int cc1 = 0, cc2 = 0;for (int i = 0; i < 26; i++){if (out[i] - in[i] == 1){cc1++;start = i;}else if (out[i] - in[i] == -1)cc2++;else if (out[i] - in[i] != 0)cc1 = 3;}if (!((cc1 == 0 && cc2 == 0) || (cc1 == 1 && cc2 == 1))){printf("***\n");continue;}cnt = 0;dfs(start);if (cnt != n){printf("***\n");continue;}for (int i = n-1; i >=0 ; i--){cout << s[ans[i]];if (i != 0) printf(".");else printf("\n");}}return 0;
}
poj 2337 Catenyms 【欧拉路径】相关推荐
- POJ 2337 输出欧拉路径
太无语了. 这道题做了一整天. 主要还是我太弱了. 以后这个就当输出欧拉路径的模版吧. 题目中的输出字典序最小我有点搞不清楚,看了别人是这么写的.但是我发现我过不了后面DISCUSS里面的数据. 题意 ...
- POJ - 2337 Catenyms 欧拉回路
测试数据在这 http://acm.student.cs.uwaterloo.ca/~acm00/030125/data/ 首先在说这道题跟之前做的poj1386很像 但是多的就是要输入路径,并且是最 ...
- 【POJ - 2337】Catenyms(欧拉图相关,欧拉通路输出路径,tricks)
题干: A catenym is a pair of words separated by a period such that the last letter of the first word i ...
- POJ 2337 欧拉回路
题意: 如果给出的单词能够首尾相接,请按字典序输出单词,中间要加'.' 否则输出三个"*". 思路: 欧拉回路 记得按字典序排序哦~ 加边的时候要倒着加.(邻接表遍历的时候是反着的 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 欧拉回路 欧拉路径
欧拉路径(瞎)定义 : 如果有一条路径使得能够走完所有的边且每一条边经过有且只有一次,这样的路径叫做欧拉路径 欧拉回路定义 : 如果有从起点出发最后回到起点的一条路径使得能够走完所有的边且每条边经过有 ...
- 图论的起源:柯尼斯堡七桥(一笔画)问题与欧拉路径/回路
柯尼斯堡七桥问题 大数学家欧拉一生中的大部分时间在俄国和普鲁士度过.1735年,他提出了著名的柯尼斯堡七桥(Seven Bridges of Königsberg)问题: 柯尼斯堡(今俄罗斯加里宁格勒 ...
- 【HDOJ图论题集】【转】
1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...
- 一系列图论问题[转]
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
最新文章
- MS IME 2007输入法
- python可视化:matplotlib学习笔记
- 性能,可伸缩性和活力
- 创建虚拟机、安装CentOS 7
- 【MySQL】MySQL Insert into select 大量锁表导致无法插入
- Python使用广度优先和深度优先两种方法遍历目录树
- mysql 返回几个组_MySQL – 控制由组返回的行
- Java虚拟机最多支持多少个线程?
- 简述malloc/free与new/delete的区别
- 使用文本编辑器编写c代码
- 【病毒查杀】CAD杀毒方法
- 如何使用Bootbox?
- 工程测量(平面高程控制网)
- 从百草园到三味书屋,从servlet到springmvc
- 《UEFI内核导读》ACPI编程入门
- 抖音一个好的标题让你轻松上热门,该怎么写好抖音标题。
- 使用FTPClient封装FtpUtil
- java中实现的十进制转换成二进制的简单代码
- Qt成功运行Zynq UltraScale+MPSoC平台上
- 编程篇 - esp 8266物联网开发板 - 给板子编写并且烧录程序, 整点有意思的效果