-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeforces_a_abc_string.cpp
112 lines (109 loc) · 1.78 KB
/
codeforces_a_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
#include<bits/stdc++.h>
#define ll long long
#define testcase(t) ll t; cin>>t; while(t--)
using namespace std;
void quickstart(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
}
int main(){
quickstart();
testcase(t){
string str;
cin >> str;
int open = 0;
int close = 0;
int choose = 0;
int n = str.length();
int flag = 0;
if(str[0] != str[n-1]){
for(int i=0; i<n; i++){
if(str[i] == str[0])
open += 1;
else if(str[i] == str[n-1])
close += 1;
else
choose += 1;
if(close > open && choose == 0){
flag = 1;
break;
}
}
if(!flag){
char op = str[0];
char cl = str[n-1];
char ch;
if(open > close){
ch = cl;
}else if(close > open){
ch = op;
}else if(close == open && choose == 0){
cout << "YES\n";
continue;
}else{
cout << "NO\n";
continue;
}
open = 0;
close = 0;
for(int i=0;i<n;i++){
if(str[i] == op){
open++;
}else if(str[i] == cl){
close++;
}else{
if(op == ch){
open++;
}else{
close++;
}
}
if(close > open){
flag=1;
break;
}
}
if(flag || open != close){
cout << "NO\n";
continue;
}else{
open = 0;
close = 0;
for(int i=n-1;i>=0;i--){
if(str[i] == op){
open++;
}else if(str[i] == cl){
close++;
}else{
if(op == ch){
open++;
}else{
close++;
}
}
if(open > close){
flag=1;
break;
}
}
if(flag){
cout << "NO\n";
continue;
}else{
cout << "YES\n";
continue;
}
}
}else{
cout << "NO\n";
continue;
}
}else{
cout << "NO\n";
continue;
}
}
}