题意:
方法:比较明显的欧拉路。可以把字母当作点,字符串string当作边,建立有向图。判断是否存在欧拉路:有向图联通,而且所有点的出度都等于入度;或者,有向图联通,有一个点的出度比入度大一,而且有一个点的入度比出度大一,其他点的出度都等于入度。注意,因为结果要求输出字典序最小的答案,需要给边排序。
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#include <fstream>
#include <cassert>
#include <cmath>
#include <sstream>
#include <time.h>
#include <complex>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define DFOR(a,b,c) for (int (a)=(b);(a)>=(c);--(a))
#define FORSQ(a,b,c) for (int (a)=(b);(a)*(a)<=(c);++(a))
#define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
#define FOREACH(a,b) for (auto &(a) : (b))
#define rep(i,n) FOR(i,0,n)
#define repn(i,n) FORN(i,1,n)
#define drep(i,n) DFOR(i,n-1,0)
#define drepn(i,n) DFOR(i,n,1)
#define MAX(a,b) a = Max(a,b)
#define MIN(a,b) a = Min(a,b)
#define SQR(x) ((LL)(x) * (x))
#define Reset(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(all(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define TC(t) while(t--)
#define forever for(;;)
#define PINF 1000000000000
#define newline '\n'
#define test if(1)if(0)cerr
using namespace std;
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef pair<double,double> dd;
typedef pair<char,char> cc;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l4;
const double pi = acos(-1.0);
const int maxa = 26;
const int maxn = 1000+1;
vector<int> g[maxa];
int in[maxa], out[maxa];
vector<string> str;
int pa[maxa], total_cnt;
int n;
int src, dest;
vector<int> path;
int findpa(int id)
{
return id==pa[id]?id:pa[id]=findpa(pa[id]);
}
void merge(int x, int y)
{
x = findpa(x), y = findpa(y);
if (x != y)
{
total_cnt -= 1;
pa[x] = y;
}
}
void init()
{
rep(i, maxa) pa[i] = i;
total_cnt = maxa;
rep(i, maxa) g[i].clear();
Reset(in, 0);
Reset(out, 0);
src = dest = -1;
path.clear();
}
inline bool valid()
{
rep(i, maxa) if (!in[i] && !out[i]) total_cnt -= 1;
if (total_cnt != 1) return false; //graph not connected;
rep(i, maxa) if (in[i] != out[i])
{
if (in[i] == out[i]+1 && dest == -1) dest = i;
else if (in[i] == out[i]-1 && src == -1) src = i;
else return false;
}
if (src == -1) rep(i, maxa) if (in[i])
{
src = i; break;
}
return true;
}
void dfs(int cur)
{
while (!g[cur].empty())
{
int id = g[cur].back();
g[cur].pop_back();
int nxt = str[id].back()-'a';
dfs(nxt);
path.pb(id);
}
}
bool compare(int i, int j)
{
return str[i]+'.' > str[j] + '.';
}
void solve()
{
init();
cin >> n; str.resize(n);
rep(i, n)
{
cin >> str[i];
int a = str[i][0]-'a', b = str[i].back()-'a';
g[a].pb(i);
out[a] += 1;
in[b] += 1;
merge(a, b);
}
if (!valid())
{
cout << "***\n";
return;
}
rep(i, maxa) rep(j, maxa) sort(g[i].begin(), g[i].end(), compare);
dfs(src);
bool first = true;
for (int i = path.size()-1; i >= 0; --i)
{
if (first) first = false;
else cout << ".";
cout << str[path[i]];
}
cout << newline;
}
int main()
{
int T; cin >> T;
repn(kase, T)
{
solve();
}
}
/*
2
5
1 2
2 3
3 4
4 5
5 6
5
2 1
2 2
3 4
3 1
2 4
*/