-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1494A - ABC String.cpp
131 lines (102 loc) · 3.06 KB
/
1494A - ABC String.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
You are given a string a, consisting of n characters, n is even. For each i from 1 to n ai is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
b is a regular bracket sequence;
if for some i and j (1=i,j=n) ai=aj, then bi=bj.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
Input
The first line contains a single integer t (1=t=1000) — the number of testcases.
Then the descriptions of t testcases follow.
The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2=n=50.
Output
For each testcase print "YES" if there exists such a string b that:
b is a regular bracket sequence;
if for some i and j (1=i,j=n) ai=aj, then bi=bj.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
Example
inputCopy
4
AABBAC
CACA
BBBBAC
ABCA
outputCopy
YES
YES
NO
NO
Note
In the first testcase one of the possible strings b is "(())()".
In the second testcase one of the possible strings b is "()()".
*/
#include<bits/stdc++.h>
using namespace std;
bool check (unordered_map <char, char> &m, string s) {
int tmp = 0;
for (auto &x: s) {
if (m[x] == '(') tmp++;
else tmp--;
if (tmp < 0) return false;
}
return tmp == 0 ? true : false;
}
void solve() {
string s;
cin >> s;
int n = s.length();
if (s[0] == s[n -1]) {
cout << "NO\n";
return;
}
unordered_map <char, char> m;
if (s[0] == 'A') m['A'] = '(';
else if (s[0] == 'B') m['B'] = '(';
else m['C'] = '(';
if (s[n - 1] == 'A') m['A'] = ')';
else if (s[n - 1] == 'B') m['B'] = ')';
else m['C'] = ')';
if (m.find('A') == m.end()) {
m['A'] = '(';
if (check(m, s)) {
cout << "YES\n"; return;
}
m['A'] = ')';
if (check(m, s)) {
cout << "YES\n"; return;
}
cout << "NO\n";
}
else if (m.find('B') == m.end()) {
m['B'] = '(';
if (check(m, s)) {
cout << "YES\n"; return;
}
m['B'] = ')';
if (check(m, s)) {
cout << "YES\n"; return;
}
cout << "NO\n";
}
else if (m.find('C') == m.end()) {
m['C'] = '(';
if (check(m, s)) {
cout << "YES\n"; return;
}
m['C'] = ')';
if (check(m, s)) {
cout << "YES\n"; return;
}
cout << "NO\n";
} else cout << "NO\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}