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?
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Print a single integer — the maximum possible size of beautiful string Nikita can get.
abba
4
bab
2
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;
}