天天看點

Codeforces Round #877 (Div. 2) B. - Nikita and string

題目連結:http://codeforces.com/contest/877/problem/B

Nikita and string

time limit per test2 seconds

memory limit per test256 megabytes

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.

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.

解題心得:

  1. 其實是一個很簡單的dp,開始還以為是一個字元串的處理,主要是要注意下,在不能夠湊成aba的情況可以空出來,當是不能再可以湊成的情況下空出。
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
char s[maxn];
int dp[][maxn];
int main()
{
    scanf("%s",s);
    int len = strlen(s);
    for(int i=;i<len;i++)
    {
        dp[][i+] = dp[][i] + (s[i] == 'a');
        dp[][i+] = max(dp[][i],dp[][i]) + (s[i] == 'b');
        dp[][i+] = max(dp[][i],max(dp[][i],dp[][i])) + (s[i] == 'a');
    }
    int Max = max(dp[][len],max(dp[][len],dp[][len]));
    printf("%d",Max);
    return ;
}