之前的求助贴被删了,再发一次
https://www.luogu.com.cn/record/121462279
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int F=10020;
struct sl1{
int poi,y;
}sack[F];
int n,tou,wei,top,flag,bg,ed,top1,tot;
int rd[F],cd[F],v[F],v1[30],out[F],f[F];
string s[F];
vector<sl1> hav[30];
void fst(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
}
bool cmp(string a,string b){
return a<b;
}
int get(int x){
if(f[x]==x) return x;
return f[x]=get(f[x]);
}
void merge(int x,int y){
f[get(x)]=get(y);
}
void badend(){
cout<<"***"<<'\n';
exit(0);
}
void dfs(int x){
if(top==n){
for(int i=1;i<=n;++i){
if(i==n){
int len=s[out[i]].length();
for(int j=0;j<len-1;j++) cout<<s[out[i]][j];
}
else cout<<s[out[i]];
}
exit(0);
}
for(int i=0;i<hav[x].size();++i){
sl1 y=hav[x][i];
if(!v1[y.y]){
v1[y.y]=1;
out[++top]=y.y;
dfs(y.poi);
v1[y.y]=0;
--top;
}
}
}
signed main(){
freopen("try.in","r",stdin);
fst();
cin>>n;
for(int i=1;i<=n;++i){
cin>>s[i];
s[i]+=".";
}
sort(s+1,s+1+n);
for(int i=1;i<=26;++i) f[i]=i;
for(int i=1;i<=n;++i){
int len=s[i].length()-1;
tou=s[i][0]-'a'+1;
wei=s[i][len-1]-'a'+1;
v[tou]=v[wei]=1;
if(get(tou)!=get(wei)) merge(tou,wei);
rd[wei]++;
cd[tou]++;
hav[tou].push_back({wei,i});
}
for(int i=1;i<=26;++i){
if(v[i]&&f[i]==i) flag++;
}
if(flag>1) badend();
for(int i=1;i<=26;++i){
if(v[i]){
if(cd[i]==rd[i]+1){
if(bg) badend();
bg=i;
}
else{
if(rd[i]==cd[i]+1){
if(ed) badend();
ed=i;
}
else if(rd[i]!=cd[i]) badend();
}
}
}
if((bg&&!ed)||(!bg&&ed)) badend();
if(!bg) bg=s[1][0]-'a'+1;
dfs(bg);
return 0;
}