Codeforces Round #306 (Div. 2)---A. Two Substrings

来源:互联网 发布:小程序企业展示源码 编辑:程序博客网 时间:2024/05/16 11:47

这道题很简单,注意ABA和BAB 既可以当做AB也可以当做BA但不可以同时当做AB,BA即可,同时注意嵌套的两个同级的for 只走一遍就可以,因为第一次如果找不到,缩小范围后更不可能找到,可以节省大量时间,防止TLE。




#include<cstdio>#include<cstdlib>#include<iostream>#include<cmath>#include<string>#include<string.h>using namespace std;char s[1000005]={0};int main(){    int flag1 = 0;    int flag2 = 0;    cin>>s;    int len = strlen(s);    for(int i = 0; i < len -1;i++)    {        if(s[i] == 'A' && s[i+1] == 'B')        {            for(int j = i+2; j < len -1 && flag1 == 0;j++)            {                if(s[j] == 'B' && s[j+1] == 'A')                {                    cout<<"YES"<<endl;                    return 0;                }            }            flag1 = 1;        }        if(s[i] == 'B' && s[i+1] == 'A')        {            for(int j = i+2; j < len -1 && flag2 == 0 ; j++)            {                if(s[j] == 'A' && s[j+1] == 'B')                {                    cout<<"YES"<<endl;                    return 0;                }            }            flag2 = 1;        }    }            cout<<"NO"<<endl;    return 0;}
A. Two Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given string s. Your task is to determine if the given strings 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 between1 and 105 consisting of uppercase Latin letters.

Output

Print "YES" (without the quotes), if strings 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

0 0