C. Perfect Keyboard

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 2626 lowercase Latin letters will be arranged in some order.

Polycarp uses the same password ss 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 ss, 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 ss, 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 TT (1T10001≤T≤1000) — the number of test cases.

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

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 2626 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
input
Copy
5
ababa
codedoca
abcda
zxzytz

abcdefghijklmnopqrstuvwxyza
output
Copy
YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO

有两种情况不可行:一个字母相邻字符数大于2的不可以;构成环的不可以
符合的就是一条链
从链的端点开始输出即可。
#include <bits/stdc++.h>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const int mod = 998244353;
const int N = 1e5+5;
inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m%n);
}
ll lcm(ll m, ll n)
{
    return m*n / gcd(m, n);
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        string s;
        cin>>s;
        map<char,bool> vis;
        for(int i=0;i<26;i++)
        {
            vis[i+'a']=0;
        }
        map<char,set<char>> mp;
        int fg=0;
        for(int i=0;i<s.size();i++)
        {
            if(i-1>=0)
                mp[s[i]].insert(s[i-1]);
            if(i+1<s.size())
                mp[s[i]].insert(s[i+1]);
            if(mp[s[i]].size()>2)
            {
                fg=1;
                break;
            }
        }
        if(fg)
        {
            cout<<"NO"<<endl;
            continue;
        } else{
            string res;

            char st='A';
            for(auto i:mp)
            {
                if(i.second.size()<=1)
                {
                    st=i.first;
                    break;
                }
            }
            if(st=='A')
            {
                cout<<"NO"<<endl;
                continue;
            }
            cout<<"YES"<<endl;
            vis[st]=1;
            for(int i=0;i<mp.size();i++)
            {
                cout<<st;
                for(auto j:mp[st])
                {
                    if(!vis[j])
                    {
                        st=j;
                        vis[st]=1;
                    }
                }
            }
            for(auto i:vis)
            {
                if(i.second==0)
                    cout<<i.first;
            }
            cout<<endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dealer/p/12939489.html