time limit per test   2 seconds
memory limit per test   256 megabytes

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.

Examples

Input
abba
Output
4
Input
bab
Output
2

Note

It the first sample the string is already beautiful.

/*************************************************
题意:
给定一个只含有'a'和'b'的字符串,要求从这个字符串中进行字符删减,使得该字符串能够由三个字符串构成。
第一个和第三个串只能含有'a'或者为空,第二个字符串只能含有'b'或者为空。问使得满足上述条件的最长的字符串长度。 分两种情况,一种不含'b',最大长度就是原长。
第二种情况含有'b',利用前缀和后缀,保存每个'b'所在位置前后'a'的个数,然后利用双重for暴力历遍任意1-2个'b',计算最大长度
Max=max(Max,head_a[i]+tot_b+back_a[j]);
其中tot_b是i,j之间'b'的个数。
***************************************************/

  

 #include "cstdio"
#include "cstring"
const int MX = + ;
char str[MX];
int f[MX], b[MX];
int b_list[MX]; int main() {
memset(str,,sizeof(str));
while(~scanf("%s",str)) {
memset(f,,sizeof(f));
memset(b,,sizeof(b));
memset(b_list,,sizeof(b_list));
int tot = ;
int sum = ;
int len = strlen(str);
for(int i = ; i < len; i++) {
if(str[i] == 'b') {
b_list[tot++] = i;
} else sum++;
f[i]=sum;
}
sum=;
for(int i = len-; i >= ; i--) {
if(str[i] == 'a') sum++;
b[i] = sum;
}
if(tot==){
printf("%d\n",len);
continue;
}
int mx=-;
for(int i = ; i < tot; i++) {
for(int j = i; j < tot; j++) {
sum = f[b_list[i]]+b[b_list[j]]++j-i;
mx=mx>sum?mx:sum;
}
}
memset(str,,sizeof(str));
printf("%d\n",mx);
}
return ;
}

Nikita and string [思维-暴力] ACM的更多相关文章

  1. codeforces Round 442 B Nikita and string【前缀和+暴力枚举分界点/线性DP】

    B. Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Codeforces Round #877 (Div. 2) B. - Nikita and string

    题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...

  3. 2019牛客多校第三场B Crazy Binary String 思维

    Crazy Binary String 思维 题意 给出01串,给出定义:一个串里面0和1的个数相同,求 满足定义的最长子序列和子串 分析 子序列好求,就是0 1个数,字串需要思考一下.实在没有思路可 ...

  4. B. Game with string 思维问题转化

    B. Game with string 思维问题转化 题意 有一个字符串 每次可以删去连续的两个同样的字符,两个人轮流删,问最后谁能赢 思路 初看有点蒙蔽,仔细看看样例就会发现其实就是一个括号匹配问题 ...

  5. 玲珑杯”ACM比赛 Round #4 1054 - String cut 暴力。学到了扫描的另一种思想

    http://www.ifrog.cc/acm/problem/1054 问删除一个字符后的最小循环节是多少. 比赛的时候想不出,不知道怎么暴力. 赛后看了别人代码才晓得.唉,还以为自己字符串还不错, ...

  6. ZOJ - 3985 - String of CCPC (思维 + 暴力)

    题意: 询问一共有有多少个CCPC,每个得1分,可以自己在任意位置添加字母,第i次添加需要耗费i-1分 思路: 既然每次添加需要耗分,添加第二个字母,相当于没有添加,所以只需要添加一次就好 先计算出原 ...

  7. HDU 5944 Fxx and string(暴力/枚举)

    传送门 Fxx and string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Othe ...

  8. Mine Number(搜索,暴力) ACM省赛第三届 G

    Mine Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Every one once played the gam ...

  9. UVALive 4423 String LD 暴力

    A - String LD Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Stat ...

随机推荐

  1. 继续沿用旧的网络访问模式Apache HTTP 客户端,防止Android9闪退

    注意位置,在application 节点里面.

  2. 微信小程序之 3d轮播(swiper来实现)

    以前写过一篇3d轮播,就是这篇,使用的方法比较笨拙,而且代码不简洁.这次发现swiper也能实现同样的效果.故记录一下. 先看看效果: wxml: <swiper previous-margin ...

  3. Entity Framework入门教程(11)---EF6中的异步查询和异步保存

    EF6中的异步查询和异步保存 在.NET4.5中介绍了异步操作,异步操作在EF中也很有用,在EF6中我们可以使用DbContext的实例进行异步查询和异步保存. 1.异步查询 下边是一个通过L2E语法 ...

  4. IE提示“Internet Explorer已限制此网页运行脚本或ActiveX控件”的解决办法

    在页面html开始标签和head开始标签中间新增一行,添加以下代码: <!-- saved from url=(0014)about:internet --> 或者 直接设置IE浏览器 工 ...

  5. UE4材质特别属生记录

    tangent space normal 切线空间法线 材质默认使用的切线空间法线,可切换为对象空间法线(彩色贴图) Separate Translucency 单独半透明 允许半透明被渲染到一个单独 ...

  6. 记事本:CSS

    css更多的是一种用来修饰HTML的语言 CSS的三种引入方式 1.行内样式:一般不会这样写,如果想选择某一个,可以用之后内部样式中更加详细的选中方式 行内的优先级最高 <p style=&qu ...

  7. thymeleaf时间格式化

    Thymeleaf模板时间格式表达式     ${#dates.format(date, 'dd/MMM/yyyy HH:mm')} 例如: <input name="enroDate ...

  8. 【Java编程思想笔记】注解--自定义注解

    文章参考自:https://www.cnblogs.com/xdp-gacl/p/3622275.html 学习网站:how2java.cn 一.自定义注解的创建过程 第一步:(元注解)   使用元注 ...

  9. (一)Java工程化--Maven基础

    Maven 读作['mevən] 翻译成中文是"内行,专家" Maven是什么 包依赖的前世今生: 原始的jar包引用--> ant --> maven. 是一种项目管 ...

  10. [Kubernetes]如何让集群为我们工作?

    前一段时间倒腾k8s的时候,写了一系列的博客,有很多人不理解那样做的意义是什么,为什么要那样做,这篇文章就尝试解释一下,在实际环境中,是如何让集群为我们工作的. 因为只研究了一个月左右的时间,认识难免 ...