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.
解题心得:
- 其实是一个很简单的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. - Nikita and string的更多相关文章
- Codeforces Round #442 (Div. 2) B. Nikita and string
题意:求最长可以分a b a为三部分子串,a b a可以为空 思路在代码里 1 #include<cstdio> 2 #include<iostream> 3 #include ...
- Codeforces Round #184 (Div. 2) E. Playing with String(博弈)
题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
- Codeforces Round #877 (Div. 2) E. Danil and a Part-time Job
E. Danil and a Part-time Job 题目链接:http://codeforces.com/contest/877/problem/E time limit per test2 s ...
- Codeforces Round #877 (Div. 2) D. Olya and Energy Drinks
题目链接:http://codeforces.com/contest/877/problem/D D. Olya and Energy Drinks time limit per test2 seco ...
- 【线段树】Codeforces Round #393 (Div. 1) C. Nikita and stack
就是给你一些元素的进栈 出栈操作,不按给定的顺序,要求你对于每次输入,都依据其及其之前的输入,判断出栈顶的元素是谁. 用线段树维护,每次push,将其位置的值+1,pop,将其位置的值-1.相当于寻找 ...
- Codeforces Round #354 (Div. 2) C. Vasya and String
题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...
- Codeforces Round #354 (Div. 2) C. Vasya and String 二分
C. Vasya and String 题目连接: http://www.codeforces.com/contest/676/problem/C Description High school st ...
随机推荐
- python学习之串口编程
# coding=utf-8import serial ser=serial.Serial('com1',9600)ser.write(b"hello")while 1: ser. ...
- Ubuntu 18.10 使用VMware克隆后,克隆后的机器再手动更改interfaces配置文件后无法启动网络的解决办法
克隆过程就略过了 配置interfaces root@client02:~# vim /etc/network/interfaces # interfaces() ) and ifdown() aut ...
- Unity小知识记录
//判断运行的平台 Application.platform == RuntimePlatform.Android EditorPrefs 编辑器储存数据 [AddComponentMenu(&quo ...
- ecshop如何增加多个产品详细描述的编辑器
在做商产品详情的时候,经常会有选项卡类似的几个产品说明,如:商品详情,商品规格,参数列表,售后服务等. Ecshop后台里面默认只有一个编辑框(器),那么我们还得自己添加几个,以下是ecshop如何增 ...
- mybatis内置二级缓存。
一.查询缓存的使用,主要是为了提供查询访问速度.将用户对同一数据的重复查询过程简化, 不再每次均从数据库查询获取结果数据,从而提高访问速度. 二.内置二级缓存... 由于MyBatista从缓存中读取 ...
- mysql 定时任务和存储过程
mysql 定时任务和存储过程 最近在做日志系统,中间用到了 mysql, 其中有一个要求: 把数据库中 7天之后的日志清除了.看到 mysql 也支持 定时任务.于是就用 mysql 来做了.下面就 ...
- mysql查询问题
需求:根据选择不同的分类id,查找到同时属于选中的分类的文章id sql语句: select result,GROUP_CONCAT(category_id) from (select categor ...
- activity间的传参
Intent有两个作用:激活组件和附带数据 激活另一个activity的方法显示意图: 1. Intent intent = new Intent(); intent.setClass ...
- Windows Server 2008 R2中上传和下载文件
在 Windows Server 2008 R2 中,使用服务器管理器来启用或禁用 Windows 功能,那在这个上面如何上传和下载文件呢? 1.在“服务器管理器”->“角色”->“web ...
- 洛谷 P1181 数列分段Section I(水题日常)
题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成连续的若干段,并且每段和不超过M(可以等于M),问最少能将其分成多少段使得满足要求. 输入输出格式 输入格式: 输入文件divide_ ...