Codeforces Round #306 (Div. 2) A. Two Substrings【字符串/判断所给的字符串中是否包含不重叠的“BA” “AB”两个字符串】

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.

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

 

【分析】:strstr函数判断AB和BA。

strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。

【代码】:

#include<bits/stdc++.h>
using namespace std;
char str[200000],*p;
int main()
{
    cin>>str;
    if( (p=strstr(str,"AB")) && (strstr(p+2,"BA")) ) puts("YES");
    else if( (p=strstr(str,"BA")) && strstr(p+2,"AB") ) puts("YES");
    else puts("NO");
    return 0;
}
View Code

 

posted @ 2017-12-02 12:30  Roni_i  阅读(205)  评论(0编辑  收藏  举报