Codeforces Round #306 (Div. 2) A
来源:互联网 发布:高分辨率软件界面模糊 编辑:程序博客网 时间:2024/05/22 04:57
题意
给一个字符串(长度<=10^5)。问其中有没有一个”BA”和一个”AB”呢?如果都有并且它们不重复(即ABA不算),输出YES。否则输出NO。
思路
一开始想简单了…..
我们扫一遍,把所有”AB”字符串中A的索引放入一个vector a,把所有”BA”字符串中B的索引放入另一个vector b。最后扫一遍两个vector,如果发现一个b的值既不是一个a的值+1,也不是那个a的值-1,那么肯定就存在不重复的”BA”和”AB”了!
代码
#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn = 100010;char s[maxn];vector<int> a;vector<int> b;int main(){ scanf("%s",s); int len = strlen(s); bool flag1 = false; bool flag2 = false; for(int i = 0 ; i < len ; i ++) { if(s[i] == 'A' && s[i+1] == 'B') { a.push_back(i); } if(s[i] == 'B' && s[i+1] == 'A') { b.push_back(i); } } for(int i = 0 ; i < a.size() ; i ++) { for(int j = 0 ; j < b.size() ; j ++) { if(a[i]!=b[j]+1 && a[i]!=b[j]-1) { printf("YES\n"); return 0; } } } printf("NO\n"); return 0;}
1 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题
- 串
- 模式匹配
- 二叉树顺序结构实现
- 二叉树链式结构
- 线索二叉树
- Codeforces Round #306 (Div. 2) A
- Codeforces Round #306 (Div. 2) B
- Codeforces Round #306 (Div. 2) C
- Jquery on() load event on a single element
- Android开发记录20-获取缓存大小和清除缓存功能
- 1009过程未解
- FFFF001
- ubuntu 取消打印队列命令
- Shell 基础