CodeForces 1303C 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 (1≤T≤10001≤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. ssconsists 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

5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza

Output

YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO

算是图论题吧

dfs+建边

#include<bits/stdc++.h>
const int maxn = 205;
int t;
int mp[maxn][maxn];
int cnt[maxn];
int vis[maxn];
using namespace std;
string s;
vector<int> ans;
void dfs(int x){
    ans.push_back(x);
    vis[x] = 1;
    for(int i = 1; i <= 26; i++){
        if(!vis[i] && mp[x][i]){
            vis[i] = 1;
            dfs(i);
        }
    }
}

int main(){
    cin >> t;
    while(t--){
        cin >> s;
        ans.clear();
        memset(mp, 0, sizeof(mp));
        memset(cnt, 0, sizeof(cnt));
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i < s.size(); i++){
            int n = s[i] - 'a' + 1;
            int m = s[i - 1] - 'a' + 1;
            mp[n][m] = 1;
            mp[m][n] = 1;
        }
        for(int i = 1; i <= 26; i++)
            for(int j = 1; j <= 26; j++)
                if(mp[i][j])
                    cnt[i]++;
        int f = 0;
        for(int i = 1; i <= 26; i++){
            //cout << cnt[i] << endl;
                if(cnt[i] >= 3)
                    f = 1;
        }
        if(f)
            cout << "NO" << endl;
        else{
            for(int i = 1; i <= 26; i++)
                if(cnt[i] <= 1 && !vis[i])
                    dfs(i);
            for(int i = 1; i <= 26; i++){
                //out << vis[i] << endl;
                if(!vis[i])
                    f = 1;
            }
            if(f)
                cout << "NO" << endl;
            else{
                cout << "YES" << endl;
                for(auto i : ans){
                    char an = i - 1 + 97;
                    cout << an;
                }
                cout << endl;
            }
        }


    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值