codeforces百题计划第一周(1)
暴力找两次,第一次找AB,然后从下一个位置找BA。
第二次搜找BA,从下一个位置找AB。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 5 int main(){ 6 7 string str;cin>>str; 8 if(str.size()<4) 9 { 10 cout <<"NO\n"; 11 return 0; 12 } 13 int i=0;int cnt; 14 while(i<str.size()) 15 { 16 if(str[i]=='A'&&i+1<str.size()&&str[i+1]=='B'){ 17 cnt=i+2;break; 18 } 19 i++; 20 } 21 while(cnt<str.size()) 22 { 23 if(str[cnt]=='B'&&cnt+1<str.size()&&str[cnt+1]=='A'){ 24 cout <<"YES\n"; 25 return 0; 26 } 27 cnt++; 28 } 29 i=0; 30 while(i<str.size()) 31 { 32 if(str[i]=='B'&&i+1<str.size()&&str[i+1]=='A'){ 33 cnt=i+2;break; 34 } 35 i++; 36 } 37 while(cnt<str.size()) 38 { 39 if(str[cnt]=='A'&&cnt+1<str.size()&&str[cnt+1]=='B'){ 40 cout <<"YES\n"; 41 return 0; 42 } 43 cnt++; 44 } 45 cout <<"NO\n"; 46 return 0; 47 }