题目链接:http://codeforces.com/contest/877/problem/B


Nikita and string

time limit per test2 seconds

memory limit per test256 megabytes

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

4

input

bab

output

2

Note

It the first sample the string is already beautiful.

In the second sample he needs to delete one of “b” to make it beautiful.


解题心得:

  1. 其实是一个很简单的dp,开始还以为是一个字符串的处理,主要是要注意下,在不能够凑成aba的情况可以空出来,当是不能再可以凑成的情况下空出。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5100;
char s[maxn];
int dp[5][maxn];
int main()
{
scanf("%s",s);
int len = strlen(s);
for(int i=0;i<len;i++)
{
dp[0][i+1] = dp[0][i] + (s[i] == 'a');
dp[1][i+1] = max(dp[1][i],dp[0][i]) + (s[i] == 'b');
dp[2][i+1] = max(dp[0][i],max(dp[1][i],dp[2][i])) + (s[i] == 'a');
}
int Max = max(dp[0][len],max(dp[1][len],dp[2][len]));
printf("%d",Max);
return 0;
}

Codeforces Round #877 (Div. 2) B. - Nikita and string的更多相关文章

  1. Codeforces Round #442 (Div. 2) B. Nikita and string

    题意:求最长可以分a b a为三部分子串,a b a可以为空 思路在代码里 1 #include<cstdio> 2 #include<iostream> 3 #include ...

  2. Codeforces Round #184 (Div. 2) E. Playing with String(博弈)

    题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...

  3. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  4. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  5. Codeforces Round #877 (Div. 2) E. Danil and a Part-time Job

    E. Danil and a Part-time Job 题目链接:http://codeforces.com/contest/877/problem/E time limit per test2 s ...

  6. Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks

    题目链接:http://codeforces.com/contest/877/problem/D D. Olya and Energy Drinks time limit per test2 seco ...

  7. 【线段树】Codeforces Round #393 (Div. 1) C. Nikita and stack

    就是给你一些元素的进栈 出栈操作,不按给定的顺序,要求你对于每次输入,都依据其及其之前的输入,判断出栈顶的元素是谁. 用线段树维护,每次push,将其位置的值+1,pop,将其位置的值-1.相当于寻找 ...

  8. Codeforces Round #354 (Div. 2) C. Vasya and String

    题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...

  9. Codeforces Round #354 (Div. 2) C. Vasya and String 二分

    C. Vasya and String 题目连接: http://www.codeforces.com/contest/676/problem/C Description High school st ...

随机推荐

  1. UVA297:Quadtrees(四分树)

    题目描述 四象树是每个内结点均有4个子结点的特殊四叉树,它可用于描述平面上黑白图像.平面上的黑白图像是32行×32列的正方形,每个格子称为1个象素,是最小的图像单位.正方形图像可分成四个相等的小正方形 ...

  2. HDU3853(期望)

    题目很水了,但是原地打转的点……虽然不难想到这个坑,但是从数学的角度来讲期望不应该算正无穷嘛……为什么算0啊? ; ; int R, C; struct gird { db ori, right, d ...

  3. Jasper_crosstab_headerPosition_columngroup header position config - (headerPosition="Stretch")

    i.e <columnGroup name="column11" height="20" totalPosition="Start" ...

  4. appium环境搭建思路

    1.appium环境是不是需要appium的一个安装包? 2.我们针对android进行测试我们是不是需要android本身的一个android 的sdk? 3.android这个本身就是java基础 ...

  5. 一款被嫌弃的字体「Comic Sans」

    这是我在其他blog上看到的字体,看到的第一眼就觉得它很有意思,但并不知道它的来历.后面google了一番,这字体叫Comic Sans,背后有不少有趣的轶事,下面贴一篇介绍它的文章. 以下内容转载自 ...

  6. PHP使用Socket发送字节流

    例如,需要发送以下数据 struct header { int  type; // 消息类型 int  length; // 消息长度 } struct MSG_Q2R2DB_PAYRESULT { ...

  7. CF1079C Playing Piano

    思路: dp. 实现: #include <bits/stdc++.h> using namespace std; ], dp[][]; int main() { int n; while ...

  8. 初识requestAnimationFrame

    转载地址:https://blog.csdn.net/vhwfr2u02q/article/details/79492303 核心概念: 1.CPU节能:在页面不刷新时不执行回调(页面在隐藏.最小化等 ...

  9. jQuery和AJAX基础

    jQuery和AJAX基础 jQuery 基础: 1.jQuery 选择器: 元素选择器:$("p"): #id 选择器:$("#test"): .class ...

  10. IOS之constraints

    anyway, you can do this with auto layout. You can do it entirely in IB as of Xcode 5.1. Let's start ...