-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_ABC_String.cpp
More file actions
108 lines (100 loc) · 1.93 KB
/
A_ABC_String.cpp
File metadata and controls
108 lines (100 loc) · 1.93 KB
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
#include <bits/stdc++.h>
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * b) / gcd(a, b)
#define GOLD ((1 + sqrt(5)) / 2)
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230;
void swaps(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void swapi(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void compute()
{
int n;
char v;
bool f;
string s;
stack<char> st;
map<char, char> m;
cin >> s;
n = s.length();
if (s[0] == s[n - 1])
{
cout << "NO\n";
return;
}
m[s[0]] = '(';
m[s[n - 1]] = ')';
if (m.find('A') == m.end())
v = 'A';
else if (m.find('B') == m.end())
v = 'B';
else
v = 'C';
m[v] = '(';
f = true;
for (char c : s)
{
if (m[c] == '(')
st.push('(');
else if (!st.empty() && st.top() == '(')
st.pop();
else
{
f = false;
break;
}
}
f = f & st.empty();
if (!f)
{
while (!st.empty())
st.pop();
m[v] = ')';
f = true;
for (char c : s)
{
if (m[c] == '(')
st.push('(');
else if (!st.empty() && st.top() == '(')
st.pop();
else
{
f = false;
break;
}
}
f = f & st.empty();
}
cout << (f ? "YES\n" : "NO\n");
}
int main()
{
#ifdef debug
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("log.txt", "w", stderr);
#endif
int t;
scanf("%d", &t);
while (t--)
{
compute();
}
#ifdef debug
fprintf(stdout, "\nTIME: %.3lf sec\n", (double)clock() / (CLOCKS_PER_SEC));
#endif
return 0;
}