# 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. iOS - 让WKWebView 支持 NSURLProtocol

    iOS8以后,苹果推出了新框架Webkit,提供了替换UIWebView的组件WKWebView.各种UIWebView的问题没有了,速度更快了,占用内存少了,一句话,WKWebView是App内部加 ...

  2. [ASP.NET MVC]视图是如何呈现的

    为了搞清楚ASP.NET MVC的请求过程,我们计划从结果追踪到源头.使用VS2012创建一个空白的ASP.NET MVC项目 然后创建一个HelloController 创建一个HelloView. ...

  3. springCloud学习之服务注册和发现

    leader让完一个简单的springcloud的demo,自己之前听说过springcloud微服务,但是没有重视.现在网上查各种资料,但是感觉不怎么样啊,还是不会,明天晚上把代码给他看,天啦,这个 ...

  4. PAT甲级1075 PAT Judge

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805393241260032 题意: 有m次OJ提交记录,总共有k道 ...

  5. PHP一个小函数

    // function makeTemp($fileName="index",$ftype=0) { $tempPath="xx/xxxx/{$fileName}.htm ...

  6. 使用Entity Framework Core需要注意的一个全表查询问题

    .NET Core 迁移工作如火如荼,今天在使用 Entity Frameowork Core(又名EF Core)时写了下面这样的 LINQ 查询表达式: .Where(u => u.Id = ...

  7. MySQL之多表查询一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习

    MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建 ...

  8. [No000013E]用VSCode写python的正确姿势

    最近在学习python,之前一直用notepad++作为编辑器,偶然发现了VScode便被它的颜值吸引.用过之后发现它启动快速,插件丰富,下载安装后几乎不用怎么配置就可以直接使用,而且还支持markd ...

  9. python模块之__future__模块

    Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动.有些改动是不兼容旧版本的,也就是在当前版本运行正常的代码,到下一个版本运行就可能不正常了.为了在低版本中可以使用高版本的新特性 ...

  10. MVC 实用架构设计(〇)——总体设计

    〇.目录 一.前言 二.结构图 三.结构说明 一.前言 一直以来都想写这个系列,但基于各种理由(主要是懒惰),迟迟没有动手.今天,趁着周末的空档,终于把系列的目录公布出来了,算是开个头,也给自己一个坚 ...