# 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 SAX解析XML

    本篇讲解一下SAX解析XML这种方式,首先来看一下它的基本介绍: SAX是一种以事件驱动的XML API,由它定义的事件流可以指定从解析器传到专门的处理程序的代码的XML结构,简单的讲,它是种解析速度 ...

  2. VS 开发必用插件

    C# outline --代码折叠 Indent Guides 虚线对齐 productivity power tools 功能集中在编辑.浏览以及其他常见的构造代码时会使用到的任务 Web Esse ...

  3. linux命令学习(6):ps命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  4. get_or_create函数

    get_or_create函数比较好用. 如果查询到就返回,如果没查询到就向数据库加入新的对象. e.g. size = Size.objects.get_or_create(sizeName=siz ...

  5. 解决webstorm拉取Vue项目时卡顿,及内存爆满问题

    最近在拉取Vue项目时总是会出现webstorm内存瞬间爆满,导致webstorm卡死,而且,有时在下载完node_modues后webstorm一直处于updating中,为此在网上找了很多方法结果 ...

  6. JavaScript外部函数调用AngularJS的函数、$scope

    x 场景: 需要在用FusionCharts画的柱状图中添加点击事件,But弹出框是Angularjs搞的,我想的是直接跳到弹出框的那个路由里,然后在弹出框的控制器中绑定数据即可: /* 点击事件 * ...

  7. [No0000CD]shell 中的单行注释和多行注释

    1. 单行注释 众所周知,#  比如想要注释:echo “ni” # echo "ni" 2. 多行注释 法一: : << ! 语句1 语句2 语句3 语句4 ! 例如 ...

  8. i-chips融合芯片分析

    适合做图像变形和融合 http://www.i-chips.co.jp/products/ma_non-deployment/c786.html 下面视频其实用了2个芯片,其中IP00C733用原始信 ...

  9. Linux之sed、awk

    Linux 之AWK 命令 简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在对数据分析并生成报告时,显得尤为强大. 简单来说awk就是把文件逐行的读入,以空格默认分隔 ...

  10. [skill][https][ssl/tls] HTTPS相关知识汇总

    结论前置: A 身份验证 证书, 服务器证书 B 密钥协商 RSA   DHE / ECDHE   PSK C 加密通信 加密通信采用对称加密,使用B阶段协商出来的密钥. B 阶段如果使用 RSA 协 ...