Codeforces550A:Two Substrings

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 test(s)
input
ABA
output
NO
input
BACFAB
output
YES
input
AXBYBXA
output
NO
Note

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".


题意:

给出一个字符串,要你判断这个字符串是不是包含BA和AB,而且BA与AB不能重叠


思路:

开始只考虑了一个方向,但是按我开始的方法一个方向考虑会有没有解决的方案,例如BABBA,于是又反向来了一次判断


#include 
#include 
char str[100005];
int main()
{
    int i,j,len;
    while(~scanf("%s",str))
    {
        len = strlen(str);
        int a1,b1,a2,b2;
        a1 = b1 = a2 = b2=-1;
        for(i = 0; i=1;)
            {
                if(a1==-1 && str[i]=='A' && str[i-1]=='B' )
                {
                    a1 = i;
                    b1 = i-1;
                    i = i-2;
                    continue;
                }
                if(a2 == -1 && str[i]=='B' && str[i-1]=='A')
                {
                    b2 = i;
                    a2 = i-1;
                    i = i-2;
                    continue;
                }
                if(a1!=-1 && a2!=-1)
                    break;
                i--;
            }

        }
        if(a1!=-1 && a2!=-1)
        {
            printf("YES\n");
        }
        else
            printf("NO\n");
    }

    return 0;
}


你可能感兴趣的:(水,codeforces)