A. Two Substrings

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/550/problem/A

 

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

ABA

Sample Output

NO

 

题意

给一个字符串判断是否存在不重合的AB和BA   

 

题解

遍历两遍进行判断,可能会超时,在一个点特判一下,

 

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
#define N 100005
char f[N];
int main()
{
    int i,j,len;
    while(scanf("%s",&f)!=EOF)
    {
        len=strlen(f);
        int k1=0,k2=0;
        for(i=0;i<len;i++)
        {
            if(f[i]=='A' && f[i+1]=='B')
            {
                k1++;
                for(j=0;j<len;j++)
                    if(j<=i-2 || j>=i+2)
                    {
                        if(f[j]=='B' && f[j+1]=='A')
                        {k2=1;break;}
                    }
            }
            if(k1>3) break;  //为防超时超过3个就可以跳出了 
        }
        if(k1!=0 && k2!=0) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

posted on 2015-06-05 19:27  erciyuan  阅读(183)  评论(0编辑  收藏  举报