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. dpkg用管道批量删除deb

    dpkg -l |grep deepin|awk '{print $2}'|xargs sudo dpkg -P

  2. JavaScript数据类型 String字符串类型的属性和方法

    属性 字符串String类型的每个实例都有一个length属性,表示字符串中的字符个数.由于字符串是不可变的,所以字符串的长度也不可变 字符串的length属性不会在for/in循环中枚举,也不能通过 ...

  3. Beamer 中的页面链接

    \documentclass[]{beamer} \usetheme{Madrid} \usenavigationsymbolstemplate{} \title{Main Title} \autho ...

  4. Flink学习(二)Flink中的时间

    摘自Apache Flink官网 最早的streaming 架构是storm的lambda架构 分为三个layer batch layer serving layer speed layer 一.在s ...

  5. Linux负载查询定位工具

    1 uptime命令,负载查询命令 02:34:03 // 当前时间up 2 days, 20:14 // 系统运行时间1 user // 正在登录用户数 而最后三个数字呢,依次则是过去 1 分钟.5 ...

  6. CSS盒模型(Box Model)

    阅读目录 1. 什么是CSS盒模型 2. IE盒模型和W3C盒模型 3. CSS3属性box-sizing 4. 关于盒模型的使用 在最初接触CSS的时候,对于CSS盒模型的不了解,撞了很多次的南墙呀 ...

  7. 软件模拟I2C通讯

    I2C协议概述,有相当详细的名词解释: 通信数量受限于地址空间和400Pf总线电容. 所有的数据传输过程中,SDA线的电平变化必须在SCL为低电平时进行,SDA线的电平在SCL线为高电平时要保持稳定. ...

  8. webhook: requestbin

    A Runscope Community Project — Learn more. RequestBin Bin URL Make a request to get started. After m ...

  9. 【原创】大数据基础之Oozie(2)使用

    命令行 $ oozie help 1 导出环境变量 $ export OOZIE_URL=http://oozie_server:11000/oozie 否则都需要增加 -oozie 参数,比如 $ ...

  10. php 安装redis php扩展

    下载文件 下载上面文件解压并拷贝至php的ext目录下 如果 PHP版本不对 接下来根据你所拟定的版本去如下这两个网址下载文件 1.http://windows.php.net/downloads/p ...