Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
Input
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Output
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
Sample Input
Input
ABAOutput
NOInput
BACFABOutput
YESInput
AXBYBXAOutput
NOHint
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO".
In the second sample test there are the following occurrences of the substrings: BACFAB.
In the third sample test there is no substring "AB" nor substring "BA".
题意:
给定一字符串,求能否找出“AB”“BA”两不重叠字符串。
啊啊啊,坑比的字符串题!!卡了三组数据TAT
附AC代码:
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int main(){ 6 string s; 7 int t=-1,v=-1,ans,temp,x,y,a,b,c,d; 8 cin>>s; 9 int len=s.size(); 10 ans=0; 11 temp=0; 12 x=0; 13 y=0; 14 for(int i=0;i<len;i++){ 15 if(s[i]==‘A‘&&s[i+1]==‘B‘){ 16 if(i!=t&&!ans){ 17 ans++; 18 t=i+1; 19 a=i; 20 break; 21 } 22 } 23 } 24 for(int i=0;i<len;i++){ 25 if(s[i]==‘B‘&&s[i+1]==‘A‘){ 26 if(i!=t&&i+1!=a&&!temp){ 27 temp++; 28 t=i+1; 29 break; 30 } 31 } 32 } 33 for(int i=0;i<len;i++){ 34 if(s[i]==‘B‘&&s[i+1]==‘A‘){ 35 if(i!=v&&!x){ 36 x++; 37 v=i+1; 38 b=i; 39 break; 40 } 41 } 42 } 43 for(int i=0;i<len;i++){ 44 if(s[i]==‘A‘&&s[i+1]==‘B‘){ 45 if(i!=v&&i+1!=b&&!y){ 46 y++; 47 v=i+1; 48 break; 49 } 50 } 51 } 52 if(ans&&temp){ 53 cout<<"YES"<<endl; 54 return 0; 55 } 56 else if(x&&y){ 57 cout<<"YES"<<endl; 58 return 0; 59 } 60 cout<<"NO"<<endl; 61 return 0; 62 }