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);

}

最新文章

  1. 【转】Windows平台下的Subversion安装配置新手指南
  2. css3控制标题字数超出的部分自动显示为“...”省略号
  3. .Net Globalization and Localization
  4. 数组map()方法和filter()方法及字符串startsWith(anotherString)和endsWith(anotherString)方法
  5. Java文件拷贝
  6. 配置SQL Server 2005 远程连接(转)
  7. js 全选全不选
  8. CSS样式补充代码
  9. ATSHA204加密认证IC
  10. 利用ZABBIX的RPC-JSON作API扩展应用示例
  11. Log4delphi使用心得
  12. CKFinder 2.4 ASP.NET 破解
  13. FTP链接mac
  14. SDL介绍
  15. gethostbyname() -- 用域名或主机名获取IP地址
  16. Xamarin相关学习预估
  17. django1.8升级1.9的几个问题
  18. vue-cli3.0
  19. PHP之ThinkPHP框架(会话)
  20. SQL Server 将查询结果导出插入的简单方式

热门文章

  1. PHP 语句和时间函数
  2. [转载]RPM中SPEC常用路径以及宏变量
  3. spring+mybatis+mina+logback框架搭建
  4. Expressions入门示例
  5. qt小程序
  6. [Robot Framework] 动态等待,提供默认的等待时间,等待时间可传可不传
  7. js网页上画图
  8. UGUI图集
  9. 2018.11.14 uoj#34. 多项式乘法(ntt)
  10. 2018.10.30 NOIP模拟 排列树(树形dp+组合数学)