Nikita and string

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 000containing only lowercase English letters "a" and "b".

Output

Print a single integer — the maximum possible size of beautiful string Nikita can get.

Example
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 ' 的串,求一个最长的子串,这个字串划分为3部分,第一和第三部分只有 a 第二部分只有 b (每一部分都可为空串)


这道题当时没有想出来,后来看了一眼别人写的思路,感觉很简单

就是说这道题既然让你去求aba串的最大长度,所以这个时候就去寻找每个b两边的a的个数,用一个数组去记录在b之前的,用量另一个数组去记录在b之后的这个a的数量.  在最后的时候再通过双重for循环从i到j之间b的个数,每找到一个就比较一次,看看这两个哪个更大一些,这种方法有个特例就是全是a的时候,这个时候你用来存储最大量的变量的值是他的初始值,所以要在最后进行一次特判,假如maxx的值是其初始值,就直接把串的长度输出就行.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char mp[6000];    //存储字符串
int main()
{
    while(~scanf("%s",mp))
    {
        int l=strlen(mp);
        int str[6009],str1[6009];
        memset(str,0,sizeof(str));  //统计b之前有多少个
        memset(str1,0,sizeof(str1));   //统计b之后有多少个
        int sum=0;          //统计在这个区间里面a的个数
        for(int i=0;i<l;i++)
        {
            if(mp[i]=='b')str[i]=sum;
            else sum++;
        }
        sum=0;
        for(int i=l-1;i>=0;i--)
        {
            if(mp[i]=='b')str1[i]=sum;
            else sum++;
        }
        int maxx=-0x3f3f3f3f;   //maxx 来保存最大长度
        for(int i=0;i<l;i++)
        {
            sum=0;
            for(int j=i;j<l;j++)
            {
                if(mp[j]=='a')continue;
                sum++;
                if((str[i]+str1[j]+sum)>maxx)
                maxx=str[i]+str1[j]+sum;
            }
        }
        if(maxx==-0x3f3f3f3f)
         maxx=l;
            printf("%d\n",maxx);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值