Codeforces Round #306 (Div. 2) A
来源:互联网 发布:unity3d源码下载 编辑:程序博客网 时间:2024/05/22 06:41
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).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
ABA
NO
BACFAB
YES
AXBYBXA
NO
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".
题意:给你一串字符串,若该字符串里含有 'AB' 和 'BA' 两个字串,并且这两个字串不重叠,如 ABA 即不可。(可有多个'AB' 和 'BA')
一开始从头到尾扫了一遍,错了,特例如:ABAXXXAB,所以倒着在扫一遍就好咯,笨办法,嘻嘻(*^__^*)
代码如下:
#include<cstdio>#include<cstring>const int N = 100005;char a[N];int main(){while(~scanf("%s", a)){int len = strlen(a);int ans = 0, tmp = 0;for(int i = 0; i < len ; i++){if(a[i] == 'B' && a[i+1] == 'A' && ans == 0){ans++;i++;}else if(a[i] == 'A' && a[i+1] == 'B' && tmp == 0){tmp++;i++;}}if(ans >= 1 && tmp >= 1) {printf("YES\n");continue;}tmp = 0, ans = 0;for(int i = len-1; i >= 0; i--){if(a[i] == 'B' && a[i-1] == 'A' && ans == 0){ans++;i--;}else if(a[i] == 'A' && a[i-1] == 'B' && tmp == 0){tmp++;i--;}}if(ans >= 1 && tmp >= 1) printf("YES\n");else printf("NO\n");}return 0;}
- Codeforces Round #306 (Div. 2) A
- Codeforces Round #306 (Div. 2) A
- Codeforces Round #306 (Div. 2) A B C
- Codeforces Round #306 (Div. 2)---A. Two Substrings
- Codeforces Round #306 (Div. 2) A B C
- Codeforces Round #306 (Div. 2) A. Two Substrings
- Codeforces Round #306 (Div. 2), problem: (A) Two Substrings
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #174 (Div. 2) Problem A
- Codeforces Round #173 (Div. 2) A.Bit++
- Codeforces Round #181 (Div. 2) A题
- Codeforces Round #184 (Div. 2) A题
- Codeforces Round #185 (Div. 2)--A,B
- Codeforces Round #172 (Div. 2) A题
- Codeforces Round #166 (Div. 2) A题
- Codeforces Round #132 (Div. 2) A题
- Codeforces Round #131 (Div. 2) A题
- Codeforces Round #133 (Div. 2) A题
- Tomcat的配置
- Maximal Square - LeetCode 221
- 三层架构--如何判断一项功能到底属于哪一程
- UI进阶 —— 收起键盘方法
- Python入门-函数的使用到程序的发布安装
- Codeforces Round #306 (Div. 2) A
- Android Studio快捷键
- [hdu]Tempter of the Bone
- HDU 不容易系列之(3)—— LELE的RPG难题
- OpenStack行业实践和发展趋势
- Android的TabHost组件-android的学习之旅(四十)
- Odroid U3 烧写镜像文件
- CCNA学习指南 IP路由
- VNR共享辞书中翻译的转码和代理