-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1003.cpp
More file actions
38 lines (36 loc) · 862 Bytes
/
1003.cpp
File metadata and controls
38 lines (36 loc) · 862 Bytes
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
#include <iostream>
using namespace std;
bool accept(string s) {
int n = s.length(), p = -1, t = -1;
for (int i = 0; i < n; i++) {
switch(s[i]) {
case 'P':
if (p == -1) p = i;
else return false;
break;
case 'T':
if (t == -1) t = i;
else return false;
break;
case 'A':
break;
default:
return false;
}
}
if (p + 2 == t && 2 * p + 3 == n && s[p + 1] == 'A') return true;
string a = s.substr(0, p);
string b = s.substr(p + 1, t - p - 2);
string c = s.substr(t + 1, n - 1 - p - t);
return accept(a + "P" + b + "T" + c);
}
int main() {
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
cout << (accept(s) ? "YES" : "NO") << endl;
}
return 0;
}