AC情况:
有点气
理解:代码搬的别的大佬的,有的地方还不太理解,得继续研究。。把单词的开头和结尾两个字母看作结点,单词是线,按这个规则构建图,先根据有多少个字母落单,判断能不能直接输出,不能的话就用深度优先搜索遍历,边排序边遍历,存到栈里,如果不能遍历全部,还是输出(比如aab,baa,ccc),否则输出栈。
#include<iostream>
#include<cstdlib>
#include<bits/stdc++.h>
#include<cstring>
#include<cstdio>
using namespace std;
char word[1005][31];
typedef struct {
char* pword;
char f;
}Word;
Word no[1005];
int M,a,b;
int mapp[26][1005];
int stackk[1005];
struct LL {
int num;
LL *pre;
}temp,*Node;
int cmp(const void *a,const void *b) {
return strcmp((*(Word *)a).pword, (*(Word *)b).pword);
}
void sort(int num) {
int i = 1, large, left, right, j;
do {
j = 0;
left = 2 * i;
right = left + 1;
large = i;
if (left <= mapp[num][0] && mapp[num][left] < mapp[num][i]) large=left;
if (right <= mapp[num][0] && mapp[num][right] < mapp[num][large]) large = right;
if (i != large) {
b = mapp[num][i];
mapp[num][i] = mapp[num][large];
mapp[num][large] = b;
i = large;
if (2 * i <= mapp[num][0])j = 1;
}
} while (j);
}
void dfs(int num) {
LL *last; last = (LL*)malloc(sizeof(LL));
int i = num;
Node = NULL;
while (mapp[i][0] != 0) {
M--;
last = (LL*)malloc(sizeof(LL));
last->num = mapp[i][1];
last->pre = Node;
Node = last;
a = no[mapp[i][1]].f - 'a';
mapp[i][1] = mapp[i][mapp[i][0]--];
sort(i);
i = a;
}
while (last != NULL) {
a = no[last->num].pword[0] - 'a';
stackk[++stackk[0]] = last->num;
Node = last;
last = last->pre;
if (mapp[a][0] != 0) dfs(a);
}
}
int main() {
int N;
scanf("%d", &N);
while (N--) {
scanf("%d", &M);
for (int i = 0; i < M; i++) {
scanf("%s", word[i]);
no[i].pword = word[i];
}
for (int i = 0; i < 26; i++) mapp[i][0] = 0;
qsort(no, M, sizeof(no[0]), cmp);
for (int i = 0; i < 29; i++) stackk[i] = 0;
b = 25;
for (int i = 0; i < M; i++) {
a = no[i].pword[0]-'a';
mapp[a][++mapp[a][0]] = i;
no[i].f = no[i].pword[strlen(no[i].pword) - 1];
stackk[a]++;
stackk[no[i].f-'a']--;
if (a < b) b = a;
}
for (int i = 0; i < 26; i++) {
if (stackk[i] == -1) { stackk[26]++; continue; };
if (stackk[i] == 1) { stackk[27]++; continue; b = i; };
if (stackk[i] == 0) stackk[28]++;
}
a = 0;
if (stackk[28] == 26 || (stackk[26] == 1 && stackk[27] == 1 && stackk[28 == 24]))a = 1;
if (!a) { printf("***\n"); continue; }
stackk[0] = 0;
dfs(b);
if(M){ printf("***\n"); continue; }
b = stackk[0];
printf("%s", no[stackk[b]].pword);
for (int i = b - 1; i > 0; i--)
printf(".%s", no[stackk[i]].pword);
printf("\n");
}
return 0;
}