-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccc05j5.cpp
45 lines (43 loc) · 1.21 KB
/
ccc05j5.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
// First attempt 2025/01/06
// Continued 2025/01/07
// Problems: BBANANASS, BBBBASSSS (multiple pairs of B and S, since currently it relies on index of first S...)
#include <bits/stdc++.h>
using namespace std;
bool is_monkey(string word){
if(word=="A") return true;
if(word[0]=='A'){
if(word[1]=='N'){
if(word.length()==2) return false;
return is_monkey(word.substr(2));
}
}
else if(word[0]=='N'){
return is_monkey(word.substr(1));
}
else if(word[0]=='B'){
if(int(word.find('S'))!=-1){
bool y = is_monkey(word.substr(1, word.find('S')-1));
if(y && word.find('S')!=word.length()-1){ // here
return is_monkey(word.substr(word.find('S')+1));
}
else if(!y){
return false;
}
else if(word.find('S')==word.length()-1){
return true;
}
}
else return false;
}
return false;
}
int main(){
while(true){
string word;
cin >> word;
if(word=="X") break;
if(is_monkey(word)) printf("YES\n");
else printf("NO\n");
}
return 0;
}