题目链接: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 【欧拉路径】的更多相关文章

  1. POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8756   Accepted: 2306 Descript ...

  2. POJ 2337 Catenyms(有向欧拉图:输出欧拉路径)

    题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串  按照 词语接龙,首尾相接  的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...

  3. POJ 2337 Catenyms

    http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...

  4. POJ 2337 Catenyms (欧拉回路)

    Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8173   Accepted: 2149 Descript ...

  5. POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)

    题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...

  6. POJ 2337 输出欧拉路径

    太无语了. 这道题做了一整天. 主要还是我太弱了. 以后这个就当输出欧拉路径的模版吧. 题目中的输出字典序最小我有点搞不清楚,看了别人是这么写的.但是我发现我过不了后面DISCUSS里面的数据. 题意 ...

  7. POJ 2337 Catenyms (欧拉图)

    本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...

  8. POJ 2337 Catenyms(有向图的欧拉通路)

    题意:给n个字符串(3<=n<=1000),当字符串str[i]的尾字符与str[j]的首字符一样时,可用dot连接.判断用所有字符串一次且仅一次,连接成一串.若可以,输出答案的最小字典序 ...

  9. Poj 2337 Catenyms(有向图DFS求欧拉通路)

    题意: 给定n个单词, 问是否存在一条欧拉通路(如acm,matal,lack), 如果存在, 输出字典序最小的一条. 分析: 这题可以看作http://www.cnblogs.com/Jadon97 ...

随机推荐

  1. [luogu] P3333 [ZJOI2013]丽洁体(贪心)

    P3333 [ZJOI2013]丽洁体 题目描述 平时的练习和考试中,我们经常会碰上这样的题:命题人给出一个例句,要我们类比着写句子.这种往往被称为仿写的题,不单单出现在小学生的考试中,也有时会出现在 ...

  2. js内置对象——Math

    Math()是JavaScript的内置对象(对于内置对象的理解,会单独写文章说明),包含了很多数学相关的方法: 常用方法: 1 Math.ceil(x) //取最近的最大整数返回 2 Math.fl ...

  3. tp volist需要便利两个数组时的处理办法

    你需要便利两个数组,并且需要使用key 和value的试的时候,volist是否先得有些捉鸡? 我们可以便利其中一个数组,而另一个利用数组的指针来操作 next($arr) 将数组指针下移 key($ ...

  4. Mysql学习总结(30)——MySQL 索引详解大全

    什么是索引? 1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引,会保存在额外的文件中. 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类 ...

  5. WinServer-PowerShell基础

    命令的参数: [-name] 这个参数必须要有,string表示name参数接受什么样的实参,<>表示参数可以接受的实参类型,通常出现set get add都会伴随着必须参数 [-name ...

  6. HDU 5187 zhx&#39;s contest(防爆__int64 )

    Problem Description As one of the most powerful brushes, zhx is required to give his juniors n probl ...

  7. 基于redis ae实现 Linux中的文件系统监控机制(inotify)

    (英文部分为转的.代码是个人代码) 1 What's inotify  The inotify API provides a mechanism for monitoring file system ...

  8. hdu 4882 ZCC Loves Codefires(贪心)

    # include<stdio.h> # include <algorithm> # include <string.h> using namespace std; ...

  9. wikioi 1306 机智Trie树

    题目描写叙述 Description 看广播操无聊得非常~你有认为吗?在看广播操一波又一波的人潮涌过再退去.认为非常没意思--于是,偶们的大神犇JHT发明了一个及其好玩的游戏~ 把每一班级的队形看成一 ...

  10. google 搜索不跳中间页

    Array.prototype.slice.call(document.links,0).forEach(function(link){link.onmousedown = null}) 插件总失效 ...