codeforces #442 B

来源:互联网 发布:手机屏幕动物软件 编辑:程序博客网 时间:2024/05/01 20:48

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.


自己代码不忍直视 贴一个巨巨的  代码好强



#include<stdio.h>#include<iostream>#include<string>#include<math.h>using namespace std;int max(int a,int b){return a>b?a:b;}int main(){    std::ios::sync_with_stdio(false);        string s;        cin>>s;        int n=s.length(),i;        int a=0,aba=0,ab=0;        for(i=0;i<n;i++)        {            if(s[i]=='a') aba++,a++;  //记录总长度最长 和 a 的长度            if(s[i]=='b')ab++;           //记录  aaaa这样的最长 还是  aaaaabbbb这种类型的 最长的长度            aba=max(aba,ab);            ab=max(ab,a);        }        cout<<aba;    return 0;}