# 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. 奇怪的git代理超时问题

    曾几何时在公司用代理上过网,后来在家里使用git訪问csdn code和github就出现代理超时的问题,例如以下: $ git clone https://github.com/bumptech/g ...

  2. 创建预编译头 Debug 正常 Release Link Error:预编译头已存在,使用第一个 PCH

    创建预编译头 Debug 正常 Release Link Error Main.obj : error LNK2005: ___@@_PchSym_@00@UmfilkilqUdrmzkkUkilqU ...

  3. java基础---->序列化框架arvo的使用

    这里面我们介绍一下序列化框架arvo的使用,在kafka中使用的就是这个. arvo的使用 一.需要通过插件生成Model类方式 一.生成我们的数据模型User.java 我们在resources里面 ...

  4. Office Web Apps Server

    Office Web Apps Server Office Web Apps Server 是一款 Office 服务器产品,可提供针对 Office 文件的基于浏览器的文件查看和编辑服务.Offic ...

  5. B - Calculation 2

    Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...

  6. spark - Locality Level

    这几个值在图中代表 task 的计算节点和 task 的输入数据的节点位置关系 PROCESS_LOCAL: 数据在同一个 JVM 中,即同一个 executor 上.这是最佳数据 locality. ...

  7. MySQL备份恢复工具Xtrabackup

    参考文档https://www.cnblogs.com/youkanyouxiao/p/8335173.html Xtrabackup介绍  Xtrabackup是由percona开源的免费数据库热备 ...

  8. triangular distribution

    mode(众数), 一组数据中出现次数最多的那个(或那些)数值. 众数可以不存在或多于一个. 例如, 1,2,3,3,4的众数是3. 1,2,2,3,3,4的众数是2和3. 1,2,3,4,5没有众数 ...

  9. easyui datagrid 取消删除的方法

    下面为取消方法 ... { field: 'Guid', title: '操作', width: 80, align: 'center', formatter: function (value, ro ...

  10. BZOJ 3224 - 普通平衡树 - [Treap][Splay]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中 ...