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. web.xml:<url-pattern>

    web.xml 中的 <url-pattern> 是 <servlet-mapping> 或 <filter-mapping> 下的子标签. url :http:/ ...

  2. mysql 连接 django

    版本: django:1.11.9 python3 mysql 5.7.18 在这里我们认为你已经安装好了mysql,python ,django 下面是来自django官方教程的一段话 If you ...

  3. Neo4j 第一篇:在Windows环境中安装Neo4j

    图形数据库(Graph Database)是NoSQL数据库家族中特殊的存在,用于存储丰富的关系数据,Neo4j 是目前最流行的图形数据库,支持完整的事务,在属性图中,图是由顶点(Vertex),边( ...

  4. 第三节:深度剖析各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字

    一. 各类数据结构比较及其线程安全问题 1. Array(数组): 分配在连续内存中,不能随意扩展,数组中数值类型必须是一致的.数组的声明有两种形式:直接定义长度,然后赋值:直接赋值. 缺点:插入数据 ...

  5. Java设计模式之抽象工厂

    概述 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设计经验的总结. 使用设计模式的目的:为了代码可重用性.让代码更容易被他人理解.保证代码可靠性. 设计模式 ...

  6. jQuery UI弹出新窗体

    借助jqueryUI 的Dialog 在隐藏的div中嵌入Iframe  改变iframe的路径 如果项目经常用到弹出新窗体,则利用模板,把此代码和html 放入父页面中,实现父级调用, <in ...

  7. luogu P5286 [HNOI2019]鱼

    传送门 这题真的牛皮,还好考场没去刚( 这题口胡起来真的简单 首先枚举D点,然后对其他所有点按极角排序,同时记录到D的距离.然后按照极角序枚举A,那么鱼尾的两个点的极角范围就是A关于D对称的那个向量, ...

  8. excel生成数据地图

    在数据分析过程中,图表是一个十分重要的部分,通过图表可以清晰明了的说明一些数字特征.在众多数据分析图表中,数据地图是常用的一中分析图.在一般的数据分析中,excel已经可以满足绝大部分功能.在本文中, ...

  9. 使用fetch调用node.js的Resuful服务

    在目前的软件架构中,慢慢又有这样的趋势,就是在前端和业务接口层中间再加入一层,这是由于nodejs相对JAVA而言不适合做复杂的业务逻辑,如下图: 在这样的结构中,JS前端和web层都是前端开发工程师 ...

  10. PDF怎样添加注释,PDF文件添加注释的方法

    Word文件跟纸质文件想要添加注释相信大家都知道该怎么添加,那么现在也使用频率挺高的PDF格式的文件要怎么添加注释呢?添加注释的方法有什么呢?有许多的小伙伴们都想知道吧,今天小编就来跟大家分享一下,想 ...