Codeforces Round #442 (Div. 2)--B. Nikita and string--前后缀和思维

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

Copy

abba

output

Copy

4

input

Copy

bab

output

Copy

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.

 

问能得到最大的ABA串,使得A只包含0-若干个a,B只包含0-若干个b。

两重暴力即可,用b的长度加上前后a的长度即可,取最大值。

 

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <string>     //字符串类
#include <vector>     //STL动态数组容器
#define ll long long
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 5000+66;
const int maxm=100000+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const int INF=99999999;
char s[maxn];
int s1[maxn];
int s2[maxn];
int main()
{
    while(scanf("%s",s+1)!=EOF)
    {
        int n=strlen(s+1);
        rep(i,1,n)
        {
            if(s[i]=='a')
                s1[i]=s1[i-1]+1;
            else
                s1[i]=s1[i-1];
            //cout<<s1[i]<<"---";
        }
        //cout<<endl;
        dep(i,n,1)
        {
            if(s[i]=='a')
                s2[i]=s2[i+1]+1;
            else
                s2[i]=s2[i+1];
            //cout<<s2[i]<<"---";
        }
        if(s1[n]==n)
        {
            printf("%d\n",n);
            continue;
        }
        //cout<<endl;
        int maxx=-1;
        rep(i,1,n)
        {
            int o=0;
            rep(j,i,n)
            {
                if(s[j]=='a')
                    continue;
                o++;
                maxx=max(maxx,o+s1[i]+s2[j]);
                //cout<<i<<" "<<j<<" "<<maxx<<endl;
            }
        }
        printf("%d\n",max(0,maxx));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值