单词拼接
时间限制:3000 ms | 内存限制:65535 KB
难度:5

描述
给你一些单词,请你判断能否把它们首尾串起来串成一串。前一个单词的结尾应该与下一个单词的道字母相同。如

aloha

dog

arachnid

gopher

tiger

rat

可以拼接成:aloha.arachnid.dog.gopher.rat.tiger

输入
第一行是一个整数N(0<N<20),表示测试数据的组数
每组测试数据的第一行是一个整数M,表示该组测试数据中有M(2<M<1000)个互不相同的单词,随后的M行,每行是一个长度不超过30的单词,单词全部由小写字母组成。

输出
如果存在拼接方案,请输出所有拼接方案中字典序最小的方案。(两个单词之间输出一个英文句号".")
如果不存在拼接方案,则输出
***

样例输入
2
6
aloha
arachnid
dog
gopher
rat
tiger
3
oak
maple
elm

样例输出
aloha.arachnid.dog.gopher.rat.tiger
***

来源
Waterloo local 2003.01.25 /POJ

上传者
张云聪

解题:判欧拉路径+连通

有向图存在欧拉路径的条件是所有点的入度等于出度,或者一个点的入度比出度大一,为终点,一点的出度比入度大一,为始点,其余的点入度等于出度。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc {
int to,next;
char str[];
};
arc e[maxn];
int head[maxn],uf[maxn],d[maxn],tot,n,m,cnt;
string str[maxn];
bool vis[maxn];
char ans[maxn][];
bool cmp(const string &a,const string &b) {
return a > b;
}
void add(int u,int v,string &s) {
e[tot].to = v;
e[tot].next = head[u];
strcpy(e[tot].str,s.c_str());
head[u] = tot++;
}
int Find(int x) {
if(x != uf[x]) uf[x] = Find(uf[x]);
return uf[x];
}
void dfs(int u,int id){
for(int i = head[u]; ~i; i = e[i].next){
if(!vis[i]){
vis[i] = true;
dfs(e[i].to,i);
}
}
if(id > -) strcpy(ans[cnt++],e[id].str);
}
int main() {
scanf("%d",&n);
while(n--) {
scanf("%d",&m);
for(int i = tot = ; i < m; ++i)
cin>>str[i];
sort(str,str+m,cmp);
memset(vis,false,sizeof(vis));
memset(head,-,sizeof(head));
for(int i = ; i < ; ++i) {
d[i] = ;
uf[i] = i;
}
for(int i = ; i < m; ++i) {
int u = str[i][] - 'a';
int v = str[i][str[i].length() - ] - 'a';
d[v]--;
d[u]++;
add(u,v,str[i]);
int tx = Find(u);
int ty = Find(v);
if(tx != ty) uf[tx] = ty;
vis[u] = vis[v] = true;
}
int root = ,st = -,a = ,b = ;
bool flag = true;
for(int i = ; i < ; ++i){
if(vis[i]){
if(i == uf[i]) root++;
if(root > ){
flag = false;
break;
}
if(st == -) st = i;
if(abs(d[i]) > ) {
flag = false;
break;
}
if(d[i] == ){
st = i;
a++;
}
if(d[i] == -) b++;
}
}
if(flag &&(a == b && b == || a == b && b == )){
memset(vis,false,sizeof(vis));
cnt = ;
dfs(st,-);
for(int i = cnt-; i >= ; i--)
printf("%s%c",ans[i],i?'.':'\n');
}else puts("***");
}
return ;
}

NYIST 99 单词拼接的更多相关文章

  1. NYOJ 99单词拼接(有向图的欧拉(回)路)

    /* NYOJ 99单词拼接: 思路:欧拉回路或者欧拉路的搜索! 注意:是有向图的!不要当成无向图,否则在在搜索之前的判断中因为判断有无导致不必要的搜索,以致TLE! 有向图的欧拉路:abs(In[i ...

  2. nyoj 99 单词拼接

    点击打开链接 单词拼接 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 给你一些单词,请你判断能否把它们首尾串起来串成一串. 前一个单词的结尾应该与下一个单词的道字母相同 ...

  3. 单词拼接(dfs/回溯/递归)

    单词拼接传送门 //单词拼接 #include<stdio.h> #include<string.h> #include<algorithm> using name ...

  4. NYOJ 单词拼接

    # include<iostream> # include<string> # include<string.h> # include<queue> # ...

  5. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  6. nyoj--99--单词拼接(欧垃图判定+输出)

    单词拼接 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 给你一些单词,请你判断能否把它们首尾串起来串成一串. 前一个单词的结尾应该与下一个单词的道字母相同. 如 alo ...

  7. 单词倒序(java)

    如何将一串单词组成的字符串倒序呢?如:" we go to school" 变成"school to go we "java代码实现: public stati ...

  8. BUUCTF-writeup

    Reverse RSA 使用openssl模块 rsa -pubin -text -modulus -in pub.key得到n值,在 factordb.com上分解大素数得到p,q值,脚本生成pri ...

  9. JavaSE知识概述集

    一.HelloWord(文档启动Java) /* 使用命令行的方式执行的时候,cmd的默认编码格式是GBK 因此在输入中文的时候需要设置文件的编码格式位ANSI,不会出现乱码错误 注意: 0.先用ja ...

随机推荐

  1. luogu2577 [ZJOI2005] 午餐 贪心

    题目大意 THU ACM小组的吃饭计划是这样的:先把所有的人分成两队,并安排好每队中各人的排列顺序,然后一号队伍到一号窗口去排队打饭,二号队伍到二号窗口去排队打饭.每个人打完饭后立刻开始吃,所有人都吃 ...

  2. 使用playonlinux安装windows软件

    转载 http://qspy.is-programmer.com/posts/40913.html Wine提供了一个用来运行Windows程序的平台.PlayOnLinux 是使用 Python 写 ...

  3. Android中关于内部存储的一些重要函数

    一.简介 Android中,你也可以通过绝对路径以JAVA传统方式访问内部存储空间.但是以这种方式创建的文件是对私有,创建它的应用程序对该文件是可读可写,但是别的应用程序并不能直接访问它.不是所有的内 ...

  4. Fisher 线性判别

    Multiplying both sides of this result by wT and adding w0, and making use of y(x)=wTx+w0 and  y(xΓ)= ...

  5. c++ 数据预处理(数据去噪,归一化)

    正态分布3σ原则,把3倍方差之外的点设想为噪声数据来排除. 归一化,将数据经过处理之后限定到一定的范围内,一般都会将数据限定到[0,1]. #include <iostream>#incl ...

  6. easyui+struts2:datagrid无法不能得到数据

    转自:https://bbs.csdn.net/topics/390980437 easyui+struts2:datagrid无法访问到指定action: userlist.jsp部分代码: XML ...

  7. 在linux查看内存的大小

    用free -m查看的结果: # free -m          total    used    free     shared buffers     cached Mem:           ...

  8. 使用curl 上传文件,multipart/form-data

    使用curl 上传文件,multipart/form-data 1. 不使用-F,curl内置multipart/form-data功能: 2. 文件内容与真实数据无关,用abc代替数据,依然可以上传 ...

  9. php header() 函数用法归纳

    301 永久重定向 <?php header('HTTP/1.1 301 Moved Permanently'); header('Location: http://www.example.co ...

  10. php xss 函数

    function xss($string) { if (is_array($string)||is_object($string)||is_resource($string)) { return '' ...