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 ...
随机推荐
- HDU4405(期望dp)
标准期望套路,很水.读题看好是到n就可以停止了. ; int n, m; db dp[maxn]; map<int, int> mp; int main() { while (~scanf ...
- ssrs 小技巧
1. 使用 RowNumber() 增加行号 =RowNumber("DataSet1") 2. 单双行 变色 =iif(RowNumber(Nothing) Mod 2, &qu ...
- c51中的bit,SBIT
在51单片机的0x20~0x2f,是bdata区既可以字节寻址又可以位寻址.用法: 1 先用bdata存储类型关键字 定义变量,注意其值就是地址 .unsigned char bdata MYBITS ...
- Memcached 未授权访问漏洞及加固
memcached是一套分布式的高速缓存系统.它以Key-Value(键值对)形式将数据存储在内存中,这些数据通常是应用读取频繁的.正因为内存中数据的读取远远大于硬盘,因此可以用来加速应用的访问. 漏 ...
- CocoaPods 提交自己的库
今想把自己写的一个view提交到CocoaPods时候,突然发现pull request被拒了,原来从去年开始就改用trunk了... 网上那些folk 在提交的pull request的教程都不可用 ...
- laravel 错误总结
1.ReflectionException (-1) Class PhotosController does not exist 原因: 资源路由的问题 ,命名空间要区分大小写,admin首字母要大写 ...
- oop典型应用,代码。
遍历获得一个实体类的所有属性名,以及该类的所有属性的值.//先定义一个类: public class User{ public string name { get; set; } public str ...
- nodejs json
var express = require('express');var router = express.Router(); /* GET home page. */ router.get('/', ...
- Java编程简介
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=3 JAVA由Sun Microsystems In ...
- Navicat for MySQL在ubuntu下运行没有反应
Step1: 打开Navicat官网,下载Navicat ,网址:http://www.navicat.com/en/download/download.html Step2:进入下载目录,解压压缩包 ...