# include<iostream>
# include<string>
# include<string.h>
# include<queue>
# include<stdio.h>
# include<math.h>
#include <algorithm>
using namespace std;
#define MAX 2005
int first[MAX],next[MAX],u[MAX],v[MAX],used[MAX],h=,ct=,in[MAX],ans[MAX];
int out[MAX],x=;
string s[MAX];
char ch;
void AddEdge(int a,int b)
{
u[h] = a;
v[h] = b;
out[a]++;
in[b]++;
next[h] = first[u[h]];
first[u[h]] = h;
h++;
} int judge(int p)
{
int count1 = ;
int count2 = ;
int vertex[];
for(int i=;i<=p;i++)
{
if(out[i]-in[i] == )
{
if(count1>=)
return -;
vertex[] = i;
count1++;
}
else if(out[i]-in[i] == - )
{
if(count2>=)
return -;
vertex[] = i;
count2++;
}
else if(out[i]-in[i] == )
{
continue;
}
else //WA原因
{
return -;
}
}
if(count1== && count2==)
{
return vertex[];
}
else if(count1== && count2==)
{
for(int i=;i<=;i++)
{
if(in[i]!= && out[i]!=)
return i;
}
}
else
return -;
}
void dfs(int k) //k是出发顶点
{
for(int i = ; i < h; i++)
{
if(u[i] == k)
{
//printf("%d %d\n------------\n",u[i],k);
if(used[i]!=)
{
used[i] = ;
dfs(v[i]);
ans[ct++] = i;
//printf("%d %d\n------------\n",ct,i);
///求出后答案是倒着的
}
}
} }
int main()
{
int m,n,i,j;
cin>>n;
while(n--)
{
cin>>m;
h = ;
x = ;
ct = ;
for(i=;i<MAX;i++)
{
first[i] = -;
next[i] = -;
used[i] = ;
in[i] = ;
out[i] = ;
} for(i=;i<=m;i++)
{
cin>>s[i];
}
sort(s+,s+m+);
for(i=;i<=m;i++)
{
int a,b;
a = s[i][] - 'a' + ;
b = s[i][ s[i].length()- ] - 'a' + ;
//printf("%d %d",a,b);
AddEdge(a,b);
}
int k,flag = ;
k = judge();
/*
printf("%d\n------------------\n",k); for(i=1;i<h;i++)
{
printf("%d %d %d\n",u[i],v[i],used[i]);
}
printf("\n------------------\n");
*/
dfs(k); if(k==-)
{
flag = ;
}
else
{
for(i=;i<h;i++)
{
if(used[i]!=)
{
flag = ;
break;
}
}
} if(flag==)
{
for(i=ct-;i>=;i--)
{
if(i!=)
cout<<s[ans[i]]<<"." ;
else
cout<<s[ans[i]];
}
cout<<endl;
}
else printf("***\n"); }
return ;
}

NYOJ 单词拼接的更多相关文章

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

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

  2. nyoj 99 单词拼接

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

  3. NYIST 99 单词拼接

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

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

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

  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. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  9. Leetcode Substring with Concatenation of All Words

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

随机推荐

  1. [Android] Java Basic : preview

    基础教学:lecture, video, lecturer: Matt Stoker Java教学:http://www.runoob.com/java/java-intro.html[菜鸟教程,非常 ...

  2. U3D对齐功能

    1,按快捷键Shift + v 的用法:    [1]先选中你要对齐的模型A,[将物体A对齐到物体B].    [2]按快捷键Shift + v ,此时将鼠标移到模型A的各个顶点处,可发现各个顶点上会 ...

  3. JS 对象的深拷贝和浅拷贝

    转载于原文:https://www.cnblogs.com/dabingqi/p/8502932.html 这篇文章是转载于上面的链接地址,觉得写的非常好,所以收藏了,感谢原创作者的分享. 浅拷贝和深 ...

  4. CF 954H Path Counting

    H. Path Counting time limit per test 5 seconds memory limit per test 256 megabytes input standard in ...

  5. 【译】python configparser中默认值的设定

    在做某一个项目时,在读配置文件中,当出现配置文件中没有对应项目时,如果要设置默认值,以前的做法是如下的: try: apple = config.get(section, 'apple') excep ...

  6. python 写入CSV出现空白行问题及拓展

    最近在学习python,要求让出表格.期间在不懂得情况下,写了些代码,运行后发现存在输入写入猴行之间存在空白行.猴发现原来问题在打开文件的方式不对. 现将我的学习交流经验分享如下: 1,自己的起初代码 ...

  7. GIAC2018全球互联网架构大会深圳站盛况回顾,定格精彩瞬间!

    6月1日至2日,由知名软件培训公司msup和高可用架构联合推出的GIAC全球互联网架构大会在深圳华侨城洲际大酒店盛大召开.来自国内外顶级互联网公司.诸多著名科技图书作者在内的71名海内外著名专家与现场 ...

  8. SMB服务简介

      1.Samba简介 Samba是种自由软件,用来让UNIX系列的操作系统与微软Windows操作系统的SMB/CIFS(Server Message Block/Common Internet F ...

  9. 应用打开其xlspptdoc等

    http://www.libxl.com/documentation.html  xls读写编辑类库libxl https://blog.csdn.net/songbob/article/detail ...

  10. centos6.5设置key登录

    1.ssh-keygen -t rsa  一路回车,当然可以设置key密码 2.cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_key ...