zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    第27课二阶构造模式(上)---------出现的背景
    第26课 静态成员函数
    第25课类的静态成员变量
    第24课经典问题解析(下)--------类的成员函数和成员变量隶属某个具体对象吗
    第24课经典问题(中)-----关于const对象的疑问
    第24课经典的问题(上)---------对象的构造顺序与析构顺序
    第23课 神秘的临时对象
    断剑重铸007
    DG on Windows 10 S: 执行任意代码
    断剑重铸006
  • 原文地址:https://www.cnblogs.com/dealer/p/12939489.html
Copyright © 2011-2022 走看看