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.

Sample test(s)
input
ABA
output
NO
input
BACFAB
output
YES
input
AXBYBXA
output
NO

找到每组AB和BA的起点坐标,保存起来,然后用最远的一个BA的坐标减去最近的一个AB,或者最远的一个AB减去一个最近的BA,如果两者的差大于等于2,那么就YES.
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <climits>
using namespace std; int main(void)
{
char s[];
int ab[],ba[];
int p_ab = ,p_ba = ; scanf("%s",s);
for(int i = ;s[i];i ++)
{
if(s[i] == 'A' && s[i + ] == 'B')
{
ab[p_ab] = i;
p_ab ++;
}
else if(s[i] == 'B' && s[i + ] == 'A')
{
ba[p_ba] = i;
p_ba ++;
}
}
if(!p_ab || !p_ba)
{
puts("NO");
return ;
}
sort(ab,ab + p_ab);
sort(ba,ba + p_ba);
if(abs(ab[] - ba[p_ba - ]) >= || abs(ba[] - ab[p_ab - ]) >= )
puts("YES");
else
puts("NO"); return ;
}

CF Two Substrings的更多相关文章

  1. CF 914F Substrings in a String——bitset处理匹配

    题目:http://codeforces.com/contest/914/problem/F 可以对原字符串的每种字母开一个 bitset .第 i 位的 1 表示这种字母在第 i 位出现了. 考虑能 ...

  2. shift and算法

    1. CF 914F Substrings in a String 大意: 给定一个串s, q个询问, (1)单点修改, (2)询问[l,r]范围内串y的出现次数. shift and算法板子题 #p ...

  3. CF 1400F x-prime Substrings 题解【AC自动机+DP】

    CF 1400F.x-prime Substrings 题意: 给定一个由\('1'\)到\('9'\)组成的字符串\(s\)和一个数\(x\),定义一个串为\(x-prime\)串,当且仅当这个串上 ...

  4. [CF Round #294 div2] D. A and B and Interesting Substrings 【Map】

    题目链接:D. A and B and Interesting Substrings 题目大意 给定26个小写字母的权值,一共26个整数(有正有负). 给定一个小写字母组成的字符串(长度10^5),求 ...

  5. CF 316G3 Good Substrings——广义后缀自动机

    题目:http://codeforces.com/contest/316/problem/G3 对询问串和模式串一起建一个后缀自动机,做出在每个串上的 right 集合大小之后枚举自动机上的每个点看看 ...

  6. CF 917A The Monster 【括号匹配】

    [链接]:CF Examples inputCopy ((?)) outputCopy 4 inputCopy ??()?? outputCopy 7 说明 For the first sample ...

  7. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  8. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  9. Codeforces CF#628 Education 8 B. New Skateboard

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

随机推荐

  1. java去掉jsp标签内容的方法

    //去掉内容的标签   public static String removeTag(String count){    try {    int tagCheck=-1;    do {     i ...

  2. ASP.NET MVC 4 中Jquery上传插件Uploadify简单使用-版本:3.2.1

    1.官网下载开发包:http://www.uploadify.com/download/,选择免费的Flash版本: 2.解压后,需要用到以下几个文件: 需要修改uploadify.css中取消上传按 ...

  3. 如何设置Grunt

    原文地址: Step 1: Install Node.js Download a Node installer and run it. Installation packages are availa ...

  4. 第一章TP-Link 703N刷OpenWrt

    1)下载编译好的固件 openwrt-ar71xx-generic-tl-wr703n-v1-squashfs-factory.bin openwrt-ar71xx-generic-tl-wr703n ...

  5. Oracle数据库定时任务配置和日志执行情况查询

    基础配置: /***************************************************************** * * 移动抵扣券快到期推送提醒 * 首次执行 : 2 ...

  6. eclipse设置默认编码格式为UTF-8

    需要设置的几处地方为: Window->Preferences->General ->Content Type->Text->JSP 最下面设置为UTF-8 Window ...

  7. java中Arrays类中,binarySearch()方法的返回值问题

    最近在复习Java知识,发现果然不经常使用忘得非常快... 看到binarySearch()方法的使用时,发现书上有点错误,于是就自己上机实验了一下,最后总结一下该方法的返回值. 总结:binaryS ...

  8. 数据库相关文章转载(1) MySQL性能优化之参数配置

    1.目的: 通过根据服务器目前状况,修改Mysql的系统参数,达到合理利用服务器现有资源,最大合理的提高MySQL性能. 2.服务器参数: 32G内存.4个CPU,每个CPU 8核. 3.MySQL目 ...

  9. iOS 蒙板,图片叠加显示漏空部分

    懒惰了一个月了,今天写写项目里遇到的一个问题. 图片a 和图片b相互叠加,a图片四周是白色的不规则图形,里面填充黑色. b图片是一张正常图片. 需求是叠加在一起,要求将b图片根据a图片的黑色形状 扣出 ...

  10. cdoj 26 遮挡判断(shadow) 水题

    遮挡判断(shadow) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/26 ...