-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABC String.cpp
64 lines (60 loc) · 1.6 KB
/
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
// https://codeforces.com/contest/1494/problem/A
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ll long long
#define pll pair<long, long>
#define vll vector<long long>
#define inf 1e18
#define range(a,b) substr(a,b-a+1)
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int main()
{
FIO;
int t;
cin >> t;
string temp, ans;
while(t--) {
cin >> temp;
if (temp[0] == temp[temp.size() - 1]) {
cout << "NO\n";
continue;
}
else {
char open = temp[0];
char close = temp[temp.size() - 1];
int ans = 0;
for (int i = 0; i < temp.size(); i++) {
if (temp[i] == open)
ans++;
else
ans--;
if (ans < 0)
break;
}
if (!ans) {
cout << "YES\n";
continue;
}
else {
ans = 0;
for (int i = 0; i < temp.size(); i++) {
if (temp[i] == close)
ans--;
else
ans++;
if (ans < 0)
break;
}
}
if (!ans)
cout << "YES\n";
else
cout << "NO\n";
}
}
return 0;
}