博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF Two Substrings
阅读量:6852 次
发布时间:2019-06-26

本文共 1530 字,大约阅读时间需要 5 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;int main(void){ char s[100005]; int ab[100005],ba[100005]; int p_ab = 0,p_ba = 0; scanf("%s",s); for(int i = 0;s[i];i ++) { if(s[i] == 'A' && s[i + 1] == 'B') { ab[p_ab] = i; p_ab ++; } else if(s[i] == 'B' && s[i + 1] == 'A') { ba[p_ba] = i; p_ba ++; } } if(!p_ab || !p_ba) { puts("NO"); return 0; } sort(ab,ab + p_ab); sort(ba,ba + p_ba); if(abs(ab[0] - ba[p_ba - 1]) >= 2 || abs(ba[0] - ab[p_ab - 1]) >= 2) puts("YES"); else puts("NO"); return 0;}
View Code

 

转载于:https://www.cnblogs.com/xz816111/p/4572781.html

你可能感兴趣的文章
dubbo源码分析-服务端发布流程-笔记
查看>>
阿里云发布Apsara SA系列混合云存储阵列
查看>>
GoJS教程:链接模版
查看>>
QListWidget方式显示缩略图
查看>>
金三银四:蚂蚁金服JAVA后端面试题及答案之二面
查看>>
Ubuntu 外网不通解决方案
查看>>
OSChina 周六乱弹 —— 历史总是惊人的相似
查看>>
MySQL 大小写
查看>>
Lync 2013部署图片赏析-证书服务安装配置
查看>>
HTML5 本地缓存 (web存储)
查看>>
tomcat redis session共享(包含redis安全设置)
查看>>
iptables中DNAT、SNAT和MASQUERADE的作用
查看>>
kvm命令学习记录
查看>>
小菜鸡进阶之路-First week
查看>>
ORACLE 10g SYSAUX表空间快速增长之WRH$_ACTIVE_SESSION_HISTORY篇
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
子数组的和的最大值(包括升级版的首尾相连数组)
查看>>
LeetCode - Nth Highest Salary
查看>>
9.ORM数据访问
查看>>