您的位置:首页 > 其它

A. Two Substrings

2015-07-03 19:01 459 查看
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


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函数即可。另外注意要求不存在叠加的情况,可以用指针实现移位。

#include<stdio.h>
#include <string.h>

char s[100001];
int main()
{
char *c;
scanf("%s",s);
if((c=strstr(s,"AB"))!=NULL && strstr(c+2,"BA")!=NULL)
{
printf("YES\n");
return 0;
}
if((c=strstr(s,"BA"))!=NULL && strstr(c+2,"AB")!=NULL)
{
printf("YES\n");
return 0;
}
printf("NO\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: