B. Nikita and string

One day Nikita found the string containing letters "a" and "b" only.

Nikita thinks that string is beautiful if it can be cut into 3 strings (possibly empty) without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b".

Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get?

Input
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".

Output
Print a single integer — the maximum possible size of beautiful string Nikita can get.

Input

abba

Output


题意:给出一个字符串,这个字符串长度最大是5000,并且规定只有ab组成,现在问你,能不能将这个字符串分成三部分,第一和第三部分只有a或者是空,第二部分由b或者空组成。问你能不能分成这样的形式,如果能那么最长的长度是多少。

思路:暴力+模拟【只有5中情况。分别是a,b,ab,ba,aba。那么我们只要统计出这五种情况的最大的长度,最后我们取其中最大的就好了】

AC代码:{调了挺久的QAQ 菜是原罪}

 #include<bits/stdc++.h>

 using namespace std;
#define N 1200000
set<char> sss;
struct str{
int type;
int num;
int qian;// 前缀
int hou;// 后缀
}st[N];
int main(){
string str;
cin>>str;
int suma=;
int sumb=;
int cnt=;
int add=;
for(int i=;i<=str.size();i++){
if(str[i]=='a'&&i!=str.size()){
suma++;
}else if(str[i]=='b'&&i!=str.size()){
sumb++;
}
if(str[i]!=str[i+]){
st[cnt].type=str[i]-'a';
st[cnt++].num=add;
add=;
}else{
add++;
}
if(i!=str.size())
sss.insert(str[i]);
}
if(sss.size()==){ // a,b情况
printf("%d\n",str.size());
return ;
}
int sa=;
int sb=;
for(int i=;i<cnt;i++){// 前缀
if(st[i].type==){
st[i].qian=sa;
}else{
st[i].qian=sb;
}
if(st[i].type==){ // 类型:B
sb+=st[i].num;
}else{
sa+=st[i].num;
}
}
sa=;
sb=;
for(int i=cnt-;i>=;i--){
if(st[i].type==){
st[i].hou=sa;
}else{
st[i].hou=sb;
}
if(st[i].type==){ // 类型:B
sb+=st[i].num;
}else{
sa+=st[i].num;
}
}
/*
for(int i=0;i<cnt;i++){
printf("%d ",st[i].num);
}
printf("\n");
for(int i=0;i<cnt;i++){
printf("%d %d \n",st[i].qian,st[i].hou);
}
*/
int ans=max(suma,sumb); for(int i=;i<cnt;i++){
if(st[i].type==){
ans=max(ans,st[i].num+st[i].hou+st[i].qian);
int add=;
for(int j=i+;j<cnt;j+=){
add+=st[j].num;
ans=max(ans,st[i].qian+add+st[i].num+st[j].hou);
}
} }
for(int i=;i<cnt;i++){
ans=max(ans,st[i].num+st[i].hou);
int res=;
for(int j=i+;j<cnt;j+=){
res+=st[j].num;
ans=max(ans,st[i].num+res+st[j].hou);
}
} cout<<ans;
return ;
} /*
bbabbbbbaaba
aabaaaaabbab
aaaabaabbbbbaaabaaaa
a,b,ab,ba,aba bb a bbbbb aa b a 2 1 5 2 1 1 */

Codeforces Round #442 (Div. 2) B题【一道模拟题QAQ】的更多相关文章

  1. Codeforces Round #575 (Div. 3) 昨天的div3 补题

    Codeforces Round #575 (Div. 3) 这个div3打的太差了,心态都崩了. B. Odd Sum Segments B 题我就想了很久,这个题目我是找的奇数的个数,因为奇数想分 ...

  2. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

  3. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  4. Codeforces Round #334 (Div. 2) A. Uncowed Forces 水题

    A. Uncowed Forces Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/pro ...

  5. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  7. Codeforces Round #345 (Div. 2)——A. Joysticks(模拟+特判)

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  8. Codeforces Round #306 (Div. 2) A. Two Substrings 水题

    A. Two Substrings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...

  9. Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题

    A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...

随机推荐

  1. MySQLdb和pymysql区别

    MySQLdb要快点,原因是这个是C写的,速度快 MySQLdb只支持Python2.x,还不支持3.x 可以用PyMySQL代替.安装方法:pip install PyMySQL 然后在需要的项目中 ...

  2. git 去除版本控制

    git会进入当前文件目录, 然后执行如下命令: find . -name ".git" | xargs rm -Rf 该项目就会去除git的版本控制了.再修改的话也不会影响git的 ...

  3. (六)lucene之其他查询方式(组合查询,制定数字范围、指定字符串开头)

    本章使用的是lucene5.3.0 指定数字范围查询 package com.shyroke.test; import java.io.IOException; import java.nio.fil ...

  4. windows环境下 快速杀死占用端口的进程

    保存为bat脚本,设置需要解除占用的端口 port,点击运行即可 @echo off setlocal enabledelayedexpansion set prot = 8022 for /f &q ...

  5. 如何把Windows主机中的文件拉到centOS虚拟机中

    如何把Windows主机中的文件拉到centOS虚拟机中 2017年02月19日 22:19:12 Ariel_lin2017 阅读数:6023 标签: vmware tools共享文件   之前写了 ...

  6. Linux增加虚拟内存

    Docker容器启动Mysql镜像报错,提示无法分配内存,报错信息如下: 由此我们看到Swap为0,考虑适当增加swap. Linux开启swap空间有好几种方法,在这里只介绍比较常用的两种. 使用交 ...

  7. RestControllerAdvice,ControllerAdvice

    1.切记@RestControllerAdvice 和 @ControllerAdvice 不能放在common里,会不生效,还会引起子项目的全局异常失败. 所以这2个还是放在各自的子项目里去处理.一 ...

  8. session过期,登录页面嵌套问题解决

    项目主页是框架模式时,如果登录后长时间没有活动(操作),存储在session中的登录信息过期了,这时再去进行操作时,就会出现登录页面嵌套的问题,怎么解决呢? 这里介绍一种方法,只需要加上一段javas ...

  9. 安装window、linux双系统

    一.安装windows,从主硬盘中分出40G空间: 二.下载linux ios镜像; 三.安装UltraISO,把下载好的linux镜像写入u盘: ‘写入硬盘镜像’->写入方式‘usb-HDD+ ...

  10. Mac下安装和配置Vue项目

    题记:学vue有一段时间了,终于今天下定决心每日书写一篇学习笔记.1.访问node.js官网:https://nodejs.org/en/ 下载对应安装包.2.安装完成,在终端输入 : node -v ...