-
1303-C, *1600코드포스 2021. 12. 26. 14:30
Problem - C - Codeforces
codeforces.com
풀이
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081#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<int, int> 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;}cs'코드포스' 카테고리의 다른 글
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