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. (转)Java程序员简历模板

    本简历模板由国内首家互联网人才拍卖网站「 JobDeer.com 」提供. (括号里的是我们的顾问编写的说明,建议在简历书写完成后统一删除) 先讲讲怎样才是一份好的技术简历 首先,一份好的简历不光说明 ...

  2. 利用spring boot构建一个简单的web工程

    1.选择Spring InitiaLizr,    jdk选择好路径 2.设置项目信息 3.这一步是设置选择使用哪些组件,这里我们只需要选择web 4.设置工程名和路径

  3. 找不到或无法加载主类(Could not find or load main class )

    在Linux环境下,写了一个简单的java程序,通过javac编译成class文件,然后用java 运行的时候,报了这个错误, 搜了一下,可能是classpath的问题,所以用echo $CLASSP ...

  4. SumatraPDF默认配置文件备份

    background color of the non-document windows, traditionally yellow MainWindowBackground = #fff200 if ...

  5. android xml 解析汉字只出来一个字的问题

    DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); // 实例化DocumentBuilder factor ...

  6. Common tasks that you can perform with the Groovy Script test step

    https://support.smartbear.com/readyapi/docs/soapui/steps/groovy.html Get test case object To obtain ...

  7. Spark学习笔记-GraphX-1

    Spark学习笔记-GraphX-1 标签: SparkGraphGraphX图计算 2014-09-29 13:04 2339人阅读 评论(0) 收藏 举报  分类: Spark(8)  版权声明: ...

  8. 【转】再讲IQueryable<T>,揭开表达式树的神秘面纱

    [转]再讲IQueryable<T>,揭开表达式树的神秘面纱 接上篇<先说IEnumerable,我们每天用的foreach你真的懂它吗?> 最近园子里定制自己的orm那是一个 ...

  9. Flex + .Net从本地选择一个图片上传到服务器

    <mx:TextInput id="TxtFileName" editable="false" width="200"/> &l ...

  10. linux系统,在centos7环境下安装jdk步骤

    记录一下安装jdk1.8版本的出错过程: 按照这个博客内容安装的,以及修改文件权限博客 [Linux]CentOS7下安装JDK详细过程 [Linux]目录文件权限的查看和修改[转] 1.安装的jdk ...