Codeforces Round #442 (Div. 2)B. Nikita and string(字符串,贪心)

9 篇文章 0 订阅
2 篇文章 0 订阅
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,最长是多少

解题思路:我们先算一个a前缀和,然后考虑到字符串的长度不大,所以我们可以暴力枚举第一部分和第二部分的边界,第二部分和第三部分的边界,然后求一下即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;

int n,i,j;
char str[5005];
int a[5005],ma,sum;
int main()
{
	cin>>str+1;
	n=strlen(str+1);
	for(i=1;i<=n+1;i++)
	{
		if(str[i]=='a')
			a[i]=a[i-1]+1;
		else
			a[i]=a[i-1];
	}
	ma = 0;
	for(i=0;i<=n+1;i++)
	{
		for(j=i+1;j<=n+1;j++)
		{
			sum=a[i]+a[n]-a[j-1]+(j-i-1)-(a[j-1]-a[i]);
			ma=max(sum,ma);
		}
	}
	cout<<ma<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值