-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA_ABC_String.cpp
More file actions
90 lines (83 loc) · 1.6 KB
/
A_ABC_String.cpp
File metadata and controls
90 lines (83 loc) · 1.6 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
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(string a, int A,int B,int C){
//cout<<A<<B<<C<<endl;
bool AA=false,BB=false,CC=false;
if(a[0]=='A') AA=true;
else if(a[0]=='B') BB=true;
else CC=true;
if(A==B+C){
B=C;
if(BB||CC) BB=CC=true;
}
else if(B==A+C){
A=C;
if(AA||CC) AA=CC=true;
}
else if(C==A+B){
B=A;
if(BB||AA) BB=AA=true;
}
//cout<<AA<<BB<<CC<<endl;
stack<char> s;
s.push('(');
for(int i=1;i<a.size();i++){
if(a[i]==a[0]){
s.push('(');
}
else if(a[i]==a[a.size()-1]){
if(s.empty()){
cout<<"NO"<<endl;
return;
}
s.pop();
}
else{
if((a[i]=='B' && BB)||(a[i]=='C' && CC)||(a[i]=='A' && AA)){
s.push('(');
}
else{
if(s.empty()){
cout<<"NO"<<endl;
return;
}
s.pop();
}
}
}
if(s.empty()){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--){
string a;
cin>>a;
int n=a.length();
int A=0,B=0,C=0;
for(int i=0;i<n;i++){
if(a[i]=='A') A++;
else if(a[i]=='B') B++;
else C++;
}
char open='(';
int x=max(A,max(B,C));
if(x!=A+B+C-x || a[0]==a[n-1]){
cout<<"NO"<<endl;
}
else{
solve(a,A,B,C);
}
}
return 0;
}