|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +const int NO_OF_ALPHABETS = 26; |
| 7 | +vector <int> degree(NO_OF_ALPHABETS, 0); |
| 8 | +vector <int> graph[NO_OF_ALPHABETS]; |
| 9 | +vector <int> component_no(NO_OF_ALPHABETS, 0); |
| 10 | + |
| 11 | +void dfs(int v, int parent_v, int c) |
| 12 | +{ |
| 13 | + //cout << "V = " << v << " C = " << c << "\n"; |
| 14 | + component_no[v] = c; |
| 15 | + |
| 16 | + for(int i = 0; i < graph[v].size(); i++) |
| 17 | + { |
| 18 | + int child_v = graph[v][i]; |
| 19 | + |
| 20 | + if(child_v == parent_v || component_no[child_v] == c) |
| 21 | + { |
| 22 | + continue; |
| 23 | + } |
| 24 | + |
| 25 | + dfs(child_v, v, c); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +void solve() |
| 30 | +{ |
| 31 | + string password; |
| 32 | + cin >> password; |
| 33 | + |
| 34 | + const int NO_OF_ALPHABETS = 26; |
| 35 | + vector <vector <int> > adjacency(NO_OF_ALPHABETS, |
| 36 | + vector <int> (NO_OF_ALPHABETS, false)); |
| 37 | + |
| 38 | + for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) |
| 39 | + { |
| 40 | + degree[alpha] = 0; |
| 41 | + |
| 42 | + graph[alpha].clear(); |
| 43 | + |
| 44 | + component_no[alpha] = 0; |
| 45 | + } |
| 46 | + |
| 47 | + for(int i = 1; i < password.size(); i++) |
| 48 | + { |
| 49 | + if(adjacency[password[i] - 'a'][password[i - 1] - 'a'] == false) |
| 50 | + { |
| 51 | + adjacency[password[i] - 'a'][password[i - 1] - 'a'] = true; |
| 52 | + adjacency[password[i - 1] - 'a'][password[i] - 'a'] = true; |
| 53 | + |
| 54 | + graph[password[i] - 'a'].push_back(password[i - 1] - 'a'); |
| 55 | + graph[password[i - 1] - 'a'].push_back(password[i] - 'a'); |
| 56 | + |
| 57 | + degree[password[i] - 'a']++; |
| 58 | + degree[password[i - 1] - 'a']++; |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) |
| 64 | + { |
| 65 | + if(degree[alpha] > 2) |
| 66 | + { |
| 67 | + cout << "NO\n"; |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + int component = 1; |
| 73 | + |
| 74 | + for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) |
| 75 | + { |
| 76 | + if(component_no[alpha] == 0) |
| 77 | + { |
| 78 | + dfs(alpha, -1, component++); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + string keyboard; |
| 83 | + vector <int> components; |
| 84 | + vector <int> used(NO_OF_ALPHABETS, false); |
| 85 | + for(int c = 1; c < component; c++) |
| 86 | + { |
| 87 | + vector <int> this_component; |
| 88 | + for(int alpha = 0; alpha < NO_OF_ALPHABETS; alpha++) |
| 89 | + { |
| 90 | + if(component_no[alpha] == c) |
| 91 | + { |
| 92 | + this_component.push_back(alpha); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if(this_component.size() == 1) |
| 97 | + { |
| 98 | + keyboard += (char)('a' + this_component[0]); |
| 99 | + used[this_component[0]] = true; |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + int border_1 = -1, border_2 = -1; |
| 104 | + for(int i = 0; i < this_component.size(); i++) |
| 105 | + { |
| 106 | + if(degree[this_component[i]] == 1) |
| 107 | + { |
| 108 | + if(border_1 == -1) |
| 109 | + border_1 = this_component[i]; |
| 110 | + else |
| 111 | + border_2 = this_component[i]; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if(border_1 == -1 || border_2 == -1) |
| 116 | + { |
| 117 | + cout << "NO\n"; |
| 118 | + |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + keyboard += (char)('a' + border_1); |
| 123 | + used[border_1] = true; |
| 124 | + |
| 125 | + while(keyboard.back() - 'a' != border_2) |
| 126 | + { |
| 127 | + for(int i = 0; i < graph[keyboard.back() - 'a'].size(); i++) |
| 128 | + { |
| 129 | + if(!used[graph[keyboard.back() - 'a'][i]]) |
| 130 | + { |
| 131 | + int next = graph[keyboard.back() - 'a'][i]; |
| 132 | + |
| 133 | + keyboard += (char)('a' + next); |
| 134 | + |
| 135 | + used[next] = true; |
| 136 | + |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + cout << "YES\n"; |
| 145 | + cout << keyboard << "\n"; |
| 146 | + return; |
| 147 | +} |
| 148 | + |
| 149 | +int main() |
| 150 | +{ |
| 151 | + int no_of_test_cases; |
| 152 | + cin >> no_of_test_cases; |
| 153 | + |
| 154 | + while(no_of_test_cases--) |
| 155 | + solve(); |
| 156 | + |
| 157 | + return 0; |
| 158 | +} |
0 commit comments