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. vue---slot,slot-scoped,以及2.6版本之后插槽的用法

    slot 插槽 ,是用在组件中,向组件分发内容.它的内容可以包含任何模板代码,包括HTML. vue 在 2.6.0 中,具名插槽和作用域插槽引入了一个新的统一的语法 (即 v-slot 指令).它取 ...

  2. Slow ReadProcessor&amp;Error Slow BlockReceiver错误日志分析(转)

    1.总结 "Slow ReadProcessor" 和"Slow BlockReceiver"往往是因为集群负载比较高或者某些节点不健康导致的,本文主要是帮助你 ...

  3. Entity Framework入门教程(17)---记录和拦截数据库命令

    记录和拦截数据库命令 这一节介绍EF6怎么记录和拦截发送给数据库的查询和操作命令. 1.记录EF发送给数据库命令(DbContext.Database.Log) 以前给了查看EF发送给数据库的命令我们 ...

  4. 第十节:基于MVC5+Unity+EF+Log4Net的基础结构搭建

    一. 前言 本节继续探讨一种新的框架搭建模式,框架的结构划分和上一节是相同的,本节IOC框架换成了Unity,并且采用构造函数注入的方式,另外服务层的封装模式也发生了变化,下面将详细的进行探讨. (一 ...

  5. 携程apollo系列-客户端集成

    本文讲解如何在 Java 程序中集成 Apollo 配置, 主要涉及到一些基础用法. 对于一些高级用法, 比如如何加密/解密配置项 (可用于数据库密码配置), 如何动态切换数据源地址,如何动态切换日志 ...

  6. [物理学与PDEs]第1章第6节 电磁场的标势与矢势 6.3 例 --- 电偶极辐射

    1. 偶极子: 相距为 $l$, 带电量分别为 $\pm q$ 的一对电荷组成的系统. 称 $$\bex {\bf m}=q{\bf l} \eex$$ 为电偶极矩, 其中 ${\bf l}$ 为 $ ...

  7. having的用法

    转载:http://blog.csdn.net/oathevil/article/details/5521757 where和having: “Where” 是一个约束声明,使用Where来约束来自于 ...

  8. VMware的NAT网络模式

    参考链接:https://www.cnblogs.com/linjiaxin/p/6476480.html 图例:

  9. 【原创】大叔经验分享(27)linux服务器升级glibc故障恢复

    redhat6系统默认安装的glibc-2.12,有的软件依赖的是glibc-2.14,这时需要升级glibc,下载安装 http://ftp.gnu.org/gnu/glibc/glibc-2.14 ...

  10. NOIP2018旅行

    这道题考场上的时候暴力写RE了,我果然很菜. 看了一篇大佬的的题解才明白 dalao的题解 但是解释很少哇,为了造福人类,在下发一篇详细一点的题解. 预处理:用vector把与每个点相连的点存起来,排 ...