Codeforces Round #877 (Div. 2) B.
来源:互联网 发布:linux新增硬盘分区命令 编辑:程序博客网 时间:2024/06/14 22:31
题目链接: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.
解题心得:
- 其实是一个很简单的dp,开始还以为是一个字符串的处理,主要是要注意下,在不能够凑成aba的情况可以空出来,当是不能再可以凑成的情况下空出。
#include<bits/stdc++.h>using namespace std;const int maxn = 5100;char s[maxn];int dp[5][maxn];int main(){ scanf("%s",s); int len = strlen(s); for(int i=0;i<len;i++) { dp[0][i+1] = dp[0][i] + (s[i] == 'a'); dp[1][i+1] = max(dp[1][i],dp[0][i]) + (s[i] == 'b'); dp[2][i+1] = max(dp[0][i],max(dp[1][i],dp[2][i])) + (s[i] == 'a'); } int Max = max(dp[0][len],max(dp[1][len],dp[2][len])); printf("%d",Max); return 0;}
- Codeforces Round #877 (Div. 2) B.
- Codeforces Round #131 (Div. 2) A B
- Codeforces Round #134 (Div. 2)B. Airport
- Codeforces Round #170 (Div. 2) problem B
- Codeforces Round #173 (Div. 2) Problem B
- Codeforces Round #181 (Div. 2) B. Coach
- Codeforces Round #185 (Div. 2)--A,B
- Codeforces Round #171 (Div. 2) B
- Codeforces Round #169 (Div. 2) B题
- Codeforces Round #188 (Div. 2) B题
- Codeforces Round #192 (Div. 2) B
- Codeforces Round #203 (Div. 2) B. Resort
- Codeforces Round #206 (Div. 2) - b
- Codeforces Round #202 (Div. 2)B-贪心
- Codeforces Round #203 (Div. 2)B
- Codeforces Round #203(Div. 2)B. Resort
- Codeforces Round #206 (Div. 2)B
- Codeforces Round #209 (Div. 2) B. Permutation
- 计算机视觉中,目前有哪些经典的目标跟踪算法?
- 文件的下载
- POJ 3162 浅谈尺取法区间问题运用及多源树上路径统计
- 多层感知机:Multi-Layer Perceptron
- Linux网络编程--(7)广播和组播
- Codeforces Round #877 (Div. 2) B.
- sql语言简单应用
- SSL2837 2017年11月6日提高组T1 互质(math)
- AVL树
- VINS技术路线与代码详解
- 用栈来进行数字的进制转换
- Oracle:时间戳
- Dialvik/ARP(ANDROID)中的多线程机制(2)
- netty对http协议解析原理解析