ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1303-C, *1600
    코드포스 2021. 12. 26. 14:30
     

    Problem - C - Codeforces

     

    codeforces.com

     

     

    풀이

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    #include <bits/stdc++.h>
    #define endl "\n"
    #define ooop(i, n) for(int i = 0; i < n; i++)
    #define loop(i, n) for(int i = 1; i <= n; i++)
    #define all(v) (v).begin(), (v).end()
     
    using namespace std;
    typedef long long ll;
    typedef pair<intint> pi;
    typedef pair<ll, ll> pl;
     
    string dfs(char head, map<char, set<char> >& edge)
    {
        vector<int> visited(26);
        string ret = "";
        queue<char> q;
        q.push(head);
        visited[head-'a'= 1;
        while(!q.empty()){
            auto cur = q.front(); q.pop();
            ret = ret + cur;
            for(auto e: edge[cur]){
                if(visited[e-'a'!= 1){
                    q.push(e);
                    visited[e-'a'= 1;
                }
            }
        }
     
        ooop(i, 26){
            if(visited[i] == 0){
                ret = ret + char(97+i);
            }
        }
     
        return ret;
    }
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        int t;
        cin >> t;
        while(t--){
            string s;
            cin >> s;
     
            if(s.size() == 1){
                string res = "";
                ooop(i, 26){
                    res = res + char(97+i);
                }
                cout << "YES" << endl << res << endl;
                continue;
            }
     
            map<char, set<char> > edge;
            ooop(i, s.size()-1){
                edge[s[i]].insert(s[i+1]);
                edge[s[i+1]].insert(s[i]);
            }
     
            bool flag = true;
            char head = '0';
            for(auto [key, s]: edge){
                if(s.size() > 2) {
                    flag = false;
                    break;
                }
                if(s.size() == 1) head = key;
            }
     
            if(flag && (head != '0')) cout << "YES" << endl << dfs(head, edge) << endl;
            else cout << "NO" << endl;
     
        }
     
        return 0;

     

    '코드포스' 카테고리의 다른 글

    1622-B  (0) 2021.12.29
    1303-D, *1900  (0) 2021.12.26
    1303-B, *1400  (0) 2021.12.26
    1615-C  (0) 2021.12.25
    1615-B  (0) 2021.12.25

    댓글

Designed by Tistory.