Codeforces Round #306 (Div. 2)---A. Two Substrings
来源:互联网 发布:小程序企业展示源码 编辑:程序博客网 时间:2024/05/16 11:47
这道题很简单,注意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 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou are given string s. Your task is to determine if the given strings 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 between1 and 105 consisting of uppercase Latin letters.
Output
Print "YES" (without the quotes), if strings 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
- Codeforces Round #306 (Div. 2)---A. Two Substrings
- Codeforces Round #306 (Div. 2) A. Two Substrings
- Codeforces Round #306 (Div. 2), problem: (A) Two Substrings
- Codeforces,306DIV.2A. Two Substrings
- Codeforces Round #306 (Div. 2) A. Two Substrings string两个函数使用
- Codeforces Round #212 (Div. 2) A A. Two Semiknights Meet
- Codeforces Round #212 (Div. 2) A. Two Semiknights Meet
- Codeforces Round #128 (Div. 2) A. Two Problems
- Codeforces Round #333 (Div. 2) A. Two Bases
- Codeforces Round #333 (Div. 2) A. Two Bases
- Codeforces Round #333 (Div. 2) A. Two Bases
- Codeforces Round #333 (Div. 2) A. Two Bases
- Codeforces Round #333 (Div. 2) A. Two Bases
- Codeforces Round #294 (Div. 2)D. A and B and Interesting Substrings
- hash(Codeforces Round #294 (Div. 2)D. A and B and Interesting Substrings)
- Codeforces Round #294 (Div. 2) -- D. A and B and Interesting Substrings
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings
- Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings(map)
- 敏捷开发之Scrum扫盲篇
- parse
- iphone6 6p纯代码适配
- IMSHello客户端的相关信息
- Android之常见错误
- Codeforces Round #306 (Div. 2)---A. Two Substrings
- Android24_LruCache缓存处理及异步加载图片类的封装
- Page 常用的方法 基础的查询
- Docker实战之入门以及Dockerfile学习笔记
- linux 下安装mysql 5.0.4的过程笔记
- 正则表达式限制输入框只能输入数字
- delphi 2010多线程使用
- CSS3 @font-face
- 第十四周 阅读程序 1