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

这道题很简单,注意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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值