-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCF_1303C.cpp
99 lines (88 loc) · 2.92 KB
/
CF_1303C.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <functional>
#include <cstdlib>
#include <list>
#include <iomanip>
#define ll long long
using namespace std;
int main() {
int T;
cin >> T;
for (int t = 0; t < T; t++) {
string str;
cin >> str;
vector<pair<char, char>> alphSet(26, { '.','.' });
int keyPos[26];
for (int i = 0; i < 26; i++) {
keyPos[i] = -1;
}
string ans;
bool isPossible = true;
for (int i = 0; i < str.size(); i++) {
char c = str[i];
if (i==0) {
ans.push_back(c);
keyPos[c - 'a'] = ans.size() - 1;
}
else {
char lastChar = str[i-1];
if (alphSet[lastChar - 'a'].first == '.' && keyPos[c-'a'] == -1) {
if (alphSet[c - 'a'].second != '.') {
isPossible = false;
}
ans.insert(ans.begin() + keyPos[lastChar - 'a'],
c);
for (int i = 0; i < 26; i++) {
if (keyPos[i] >= keyPos[lastChar - 'a']) {
keyPos[i]++;
}
}
keyPos[c - 'a'] = keyPos[lastChar - 'a'] - 1;
alphSet[c - 'a'].second = lastChar;
alphSet[lastChar - 'a'].first = c;
}
else if (alphSet[lastChar - 'a'].second == '.' && keyPos[c - 'a'] == -1) {
if (alphSet[c - 'a'].second != '.') {
isPossible = false;
}
ans.insert(ans.begin() + keyPos[lastChar - 'a']+1,
c);
for (int i = 0; i < 26; i++) {
if (keyPos[i] > keyPos[lastChar - 'a']) {
keyPos[i]++;
}
}
keyPos[c - 'a'] = keyPos[lastChar - 'a'] + 1;
alphSet[c - 'a'].first = lastChar;
alphSet[lastChar - 'a'].second = c;
}
else {
if (!(alphSet[lastChar - 'a'].first == c ||
alphSet[lastChar - 'a'].second == c)) {
isPossible = false;
}
}
}
}
if (isPossible) {
for (int i = 0; i < 26; i++) {
if (keyPos[i] == -1) {
ans.push_back(i + 'a');
}
}
cout << "YES" << endl;
cout << ans << endl;
}
else {
cout << "NO" << endl;
}
}
}