题目链接: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;
}

最新文章

  1. MongoDB集群配置
  2. ssh免密码认证
  3. dvwa第一次接触
  4. 【转】SQL SERVER标量表达式的隐式转换
  5. 怎么查看其它apk里面的布局代码及资源
  6. Flex Alert.show()方法的详解
  7. Ural 1519. Formula 1 优美的插头DP
  8. 在Android中使用枚举注解而不是枚举
  9. hdu 1151 Air Raid DAG最小边覆盖 最大二分匹配
  10. nginx配置 location及rewrite规则详解
  11. monitor.go
  12. Java Web 笔试(面试)题
  13. json.dumps与json.loads实例
  14. memcached 一致性hash原理
  15. 邮件服务器日志:/var/log/maillog
  16. python标准库介绍——22 UserList 模块详解
  17. 利用css3的text-shadow属性实现文字阴影乳白效果
  18. COGS 有标号的二分图计数系列
  19. windows下流媒体nginx-rmtp-module服务器搭建及java程序调用fmpeg将rtsp转rtmp直播流【转】
  20. OneNote如何使用

热门文章

  1. hadoop中HDFS文件系统 nameNode出现的问题 nameNode无法打开
  2. os.getcwd()函数的用法
  3. Python破解Wifi密码思路
  4. pytorch 7 save_reload 保存和提取神经网络
  5. Vue基础操作
  6. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] E】Short Code
  7. dubbo Failed to check the status of the service com.user.service.UserService. No provider available for the service
  8. 简单谈谈MySQL优化利器-慢查询
  9. android启动第一个界面时即闪屏的核心代码(两种方式)
  10. OpenCV特征点检測------Surf(特征点篇)