# Codeforces 877 B. Nikita and string （技巧）

## Description

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


## Examples output

4


## AC 代码

#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
#define inf 0x7f7f7f7f
using namespace std;
typedef __int64 LL;
const int maxn = 1e5+10;

char str[maxn];
int prea[maxn],preb[maxn];

void solve()
{
int len = strlen(str+1);
for(int i=1; i<=len; i++)
{
prea[i] = prea[i-1];
preb[i] = preb[i-1];
if(str[i]=='a')
prea[i]++;
else
preb[i]++;
}
int ans = 0;
for(int i=0; i<=len; i++)
{
for(int j=i; j<=len; j++)
{
int now = preb[i]+preb[len]-preb[j]+prea[j]-prea[i];
ans = max(ans,len - now);
}
}
printf("%d\n",ans);
}

int main()
{
gets(str+1);
solve();
return 0;
}