B. Nikita and string
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

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

思路:记录每个位置后面有多少个a,有多少个b      存在arr[5005][2]里面  arr[i][0]表示i后面包括i有arr[i][0]个a    arr[i][1]表示i后面包括i有arr[i][1]个b

遍历每个段位

for(i =0 ; i < strlen(str);++i)

{

  if(str[j] == 'b')

  {

    for(j = i; j < strlen(str);++j)

    {

       if(str[j] == 'b')

         maxn = max(maxn,  arr[0][0] - arr[i][0]    +    arr[i][2] - arr[j][2] + 1      + arr[j][0]);

    }

  }

}

注意全是a的情况

丑陋的代码

#include <iostream>

#include <cstdio>

#include <cstring>

#include <string>

using namespace std;

int main()

{

//    char str[5010];

string str;

int arr[5010][2];

int i,j,k,a,b,a1,b1;

//    memset(str,0,sizeof(str));

int maxn = 0;

a1 = b1 = a = b = 0;

//    scanf("%s",str);

cin >> str;

memset(arr, 0, sizeof(arr));

for(i = 0; i < str.length(); ++i)

{

if(str[i] == 'a')

a++;

else

b++;

}

if(b == 0)

{

printf("%d\n",(int)str.length());

return 0;

}

a1 = b1 = 0;

for(i = 0; i < str.length(); ++i)

{

if(str[i] == 'a')

{

arr[i][0] = a - a1;

arr[i][1] = b - b1;

a1++;

}

else

{

arr[i][0] = a - a1;

arr[i][1] =  b - b1;

b1++;

}

}

a1 = b1 = 0;

for(i = 0; i < str.length(); ++i)

{

if(str[i] == 'b')

{

for(j = i; j < str.length(); ++j)

{

if(str[j] == 'b')

maxn = max(maxn,a - arr[i][0] + arr[i][1] - arr[j][1] + arr[j][0]+1);

}

}

}

printf("%d\n",maxn);

}

codeforces 877b的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. C# 获取 存储过程 返回值

    C#获取存储过程的返回值,这一方法,总是容易忘,今天给贴出来,以方便下次使用 存储过程: CREATE  PROCEDURE [dbo].[Proc_GetInfo]     ),     ) out ...

  2. Maven 系列 二 :Maven 常用命令,手动创建第一个 Maven 项目

    1.根据 Maven 的约定,我们在D盘根目录手动创建如下目录及文件结构: 2.打开 pom.xml 文件,添加如下内容: 1 <project xmlns="http://maven ...

  3. hdu 1495 (搜索) 非常可乐

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 搜索模拟出每此倒得情况就好,详情见代码\ (好困啊!!!!1) #include<cstdio> ...

  4. Centos7 上安装 FastDFS

    1.安装gcc(编译时需要) FastDFS是C语言开发,安装FastDFS需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc yum install -y gcc ...

  5. oracle sqlplus操作

    步骤: su - oracle 切换到oracle用户 sqlplus /nolog 进入sqlplus命令行 conn cps/cps 连接到cps用户的数据库,cps/cps表示:用户/密码 执行 ...

  6. P1083龙舟比赛

    题目如下: 现在正在举行龙舟比赛,我们现在获得了最后冲刺时的俯视图像,现在你要输出各条龙舟的名次. 这张图像由r行c列的字符组成,每行的最左边的字符表示起点,所以字符为'S',最右边的字符为'F'.并 ...

  7. 格式化输出之 每行N个元素 list拆分

    for i in range(0,len(XSECT),5): out_BLDdat.write(' '.join( map('{:10.4f}'.format,XSECT[i:i+5]) ) + ' ...

  8. Laravel policy 的应用

    Laravel 提供更简单的方式来处理用户授权动作.类似用户认证,有 2 种主要方式来实现用户授权:gates 和策略,我这里主要讲解下策略的使用. 文档 上面有详细的说明,我这里只根据自己使用过程做 ...

  9. 轻博客类Web原型制作分享——Tumblr

    Tumblr(汤博乐)成立于2007年,是目前全球最大的轻博客网站,也是轻博客网站的始祖. Tumblr是一种介于传统博客和微博之间的全新媒体形态,既注重表达,又注重社交,而且注重个性化设置,成为当前 ...

  10. PHP字符串替换函数

    str_replace函数 描述:实现字符串替换,区分大小写 语法:mixed str_replace(mixed $search, mixed replace, mixed $subject, [i ...