Day 4 -E - Catenyms POJ - 2337
dog.gopher
gopher.rat
rat.tiger
aloha.aloha
arachnid.dog
A compound catenym is a sequence of three or more words separated by periods such that each adjacent pair of words forms a catenym. For example,
aloha.aloha.arachnid.dog.gopher.rat.tiger
Given a dictionary of lower case words, you are to find a compound catenym that contains each of the words exactly once.
Input
Output
Sample Input
2
6
aloha
arachnid
dog
gopher
rat
tiger
3
oak
maple
elm
Sample Output
aloha.arachnid.dog.gopher.rat.tiger
*** 思路:
可以将每个单词看作是两点一边,就变成判断欧拉路径(回路)了,从连通性和出度入度两方面判断,然后打印欧拉路径即可,代码如下(注释
int in[], out[], ans[], start, fa[], n, k;
struct edge {
int u, v, vis;
string s;
bool operator<(const edge &a) const {
return s<a.s;
}
} Edge[];
void init() {
memset(in, , sizeof(in)), memset(out, , sizeof(out));
memset(ans, , sizeof(ans));
k = ;
for(int i = ; i <= ; ++i)
fa[i] = i;
}
// Find-Union
int Find(int u) {
if(fa[u] != u)
return fa[u] = Find(fa[u]);
return fa[u];
}
void Union(int x, int y) {
x = Find(x), y = Find(y);
if(x != y) fa[x] = y;
}
// judge whether connected
bool Connect() {
int now = Find(start);
for(int i = ; i <= ; ++i)
if(in[i] || out[i])
if(Find(i) != now)
return false;
return true;
}
// judge the in and out and set the start
bool check() {
int t1 = , t2 = , i;
for(i = ; i <= ; ++i) {
if(in[i] == out[i]) continue;
else if(in[i] == out[i] + )
t1++;
else if(in[i] == out[i] - ) {
t2++;
start = i;
}
else
break;
}
if(i == && t1 == t2 && (t1 == || t1 == )) {
return true;
}
return false;
}
//print the euler path
void dfs(int v) {
for(int i = ; i <= n; ++i) {
if(Edge[i].vis == && Edge[i].u == v) {
Edge[i].vis = ;
dfs(Edge[i].v);
ans[k++] = i;
}
}
}
int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--) {
cin >> n;
init();
for(int i = ; i <= n; ++i) {
int u, v;
cin >> Edge[i].s;
u = Edge[i].s[] - 'a' + ;
v = Edge[i].s[Edge[i].s.size()-] - 'a' + ;
in[v]++, out[u]++;
Edge[i].u = u, Edge[i].v = v, Edge[i].vis = ;
Union(u, v);
}
sort(Edge+, Edge++n);
start = Edge[].u;
if(check() && Connect()) {
dfs(start);
for(int i = n-; i >= ; --i)
cout << Edge[ans[i]].s << ".";
cout << Edge[ans[]].s << "\n";
} else {
cout <<"***\n";
}
}
return ;
}
Day 4 -E - Catenyms POJ - 2337的更多相关文章
- Catenyms POJ - 2337(单词+字典序输出路径)
题意: 就是给出几个单词 看能否组成欧拉回路或路径 当然还是让输出组成的最小字典序的路 解析: 还是把首尾字母看成点 把单词看成边 记录边就好了 这题让我对fleury输出最小字典序又加深了一些 ...
- POJ 2337 Catenyms
http://poj.org/problem?id=2337 题意: 判断给出的单词能否首尾相连,输出字典序最小的欧拉路径. 思路: 因为要按字典序大小输出路径,所以先将字符串排序,这样加边的时候就会 ...
- POJ 2337 Catenyms(欧拉回(通)路:路径输出+最小字典序)
题目链接:http://poj.org/problem?id=2337 题目大意:给你n个字符串,只有字符串首和尾相同才能连接起来.请你以最小字典序输出连接好的单词. 解题思路:跟POJ1386一个意 ...
- poj 2337 Catenyms 【欧拉路径】
题目链接:http://poj.org/problem?id=2337 题意:给定一些单词,假设一个单词的尾字母与还有一个的首字母同样则能够连接.问能否够每一个单词用一次,将全部单词连接,能够则输出字 ...
- POJ 2337 Catenyms (有向图欧拉路径,求字典序最小的解)
Catenyms Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8756 Accepted: 2306 Descript ...
- 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(有向欧拉图:输出欧拉路径)
题目链接>>>>>> 题目大意: 给出一些字符串,问能否将这些字符串 按照 词语接龙,首尾相接 的规则 使得每个字符串出现一次 如果可以 按字典序输出这个字符串 ...
- POJ 2337 Catenyms (欧拉图)
本文链接http://i.cnblogs.com/EditPosts.aspx?postid=5402042 题意: 给你N个单词,让你把这些单词排成一个序列,使得每个单词的第一个字母和上一个字单词的 ...
随机推荐
- 4.ORM框架的查询
创建表对应关系代码如下: from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy app=Fl ...
- shell coding about mac ox
1, mac path---http://blog.csdn.net/playstudy/article/details/50149021 Mac系统的环境变量,加载顺序为:/etc/profile ...
- 丰田开放混动专利后,真能PK赢纯电动汽车吗?
特斯拉已成为美国汽车市场增速最快的厂商,且在中国建设工厂后又巩固了自身的地位:蔚来.小鹏等互联网造车企业迅速崛起,吸引着风投的强烈关注:全球范围内,纯电动汽车的销量节节攀升--从多个维度看,纯电动汽车 ...
- Python学习 —— 实现简单的爬虫
为了加快学习python3.x,查了许多资料后写了这个脚本,这个脚本主要是爬取百度图片'东方幻想乡'的图片,但还是有很多问题存在. 下面给出代码: # 更新了一下代码 from urllib impo ...
- OBST(最优二叉搜索树)
简述一下问题:假设有一颗词典二叉树,我们从中查找需要的单词,使用红黑树或平衡树这样的数据结构总是可以在O(lgN)时间内进行查找,但单词的出现频率是不同的,我们给每个单词加上一个搜索概率,然后通过这些 ...
- springboot#下载文件
膜拜大神 这就是我要的滑板鞋! @RequestMapping(value = "/media", method = RequestMethod.GET) public Respo ...
- node.js中的事件轮询Event Loop
任务队列/事件队列 "任务队列"是一个事件的队列,IO设备完成一项任务,就在"任务队列"中添加一个事件,表示相关的异步任务可以进入"执行栈" ...
- 思科 ASA 系列防火墙 官方文档下载指南
思科 ASA 系列命令参考 思科 ASA 系列命令参考,A 至 H 命令 思科 ASA 系列命令参考, I 至 R 命令 思科 ASA 系列命令参考,S 命令 思科 ASA 系列命令参考, ASASM ...
- mybatis动态参数查询
参考:https://blog.csdn.net/zbw18297786698/article/details/53727594
- Spring mvc mybatis 查询结果缺少字段 解决方法
参考:https://blog.csdn.net/xiaofeifei8421/article/details/43231815