C. Perfect Keyboard
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all 26 lowercase Latin letters will be arranged in some order.

Polycarp uses the same password s on all websites where he is registered (it is bad, but he doesn’t care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn’t like to move his fingers while typing the password, so, for each pair of adjacent characters in s, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi… is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in s, so, for example, the password cannot be password (two characters s are adjacent).

Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?

Input
The first line contains one integer T (1≤T≤1000) — the number of test cases.

Then T lines follow, each containing one string s (1≤|s|≤200) representing the test case. s consists of lowercase Latin letters only. There are no two adjacent equal characters in s.

Output
For each test case, do the following:

if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
otherwise, print YES (in upper case), and then a string consisting of 26 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.
Example
inputCopy
5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
outputCopy
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO

题意:给你一个字符串s,现在要求你构造一个26个小写字母的一个排列t,使得给出的字符串s中,相邻字母在你构造的排列t中也是相邻的 有就输出YES 并输出构造的排列 没有就输出NO

思路:把字符串s预处理一下,g[s[i]][s[i+1]]=1,表示s[i]和s[i+1]相邻,然后我们去计算每个字母的相邻字母有几个,我们知道 最多有两个 所以大于2个的直接输出NO return就行
不然去dfs,去深搜相邻只有一个字母的字母,因为有两个的是固定的不用管,没有相邻的,说明给出的字符串s没有该字母,只管往后放即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second
int v[200],c[200];
char a[30];
int g[200][200];
int flag,cnt;
void dfs(char x)
{
    if(flag)
        return ;
    if(v[x])
        flag=1;
    v[x]=1;
    for(char i='a'; i<='z'; i++)
    {
        if(g[x][i]&&v[i]==0)
        {
            a[++cnt]=i;
            dfs(i);
        }
    }
}
void solve()
{
    me(g,0);
    string s;
    cin>>s;
    int n=s.size();
    for(int i=0; i<n-1; i++)
    {
        g[s[i]][s[i+1]]=1;
        g[s[i+1]][s[i]]=1;
    }
    flag=0;
    memset(c,0,sizeof(c));
    for(char i='a'; i<='z'; i++)
    {
        for(char j='a'; j<='z'; j++)
        {
            if(g[i][j])
                c[i]++;
        }
        if(c[i]>2)
        {
            puts("NO");
            return ;
        }
    }
    cnt=0;
    me(v,0);
    for(char i='a'; i<='z'; i++)
    {
        if(c[i]==0)
            a[++cnt]=i;
        else if(c[i]==1&&v[i]==0)
        {
            a[++cnt]=i;
            dfs(i);
        }
        else if(c[i]==2)
            continue;
    }
    if(cnt!=26)
    {
        puts("NO");
    }
    else
    {
        puts("YES");
        for(int i=1; i<=cnt; i++)
            cout<<a[i];
        puts("");
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }

    return 0;
}