codefforces 877B
B. Nikita and string
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
(这一题做的时候,一直不理解 without changing their order 是什么意思,我的理解是只能从最左边和最右边删除字符。。。
题意:给一个只含字符a,b的字符串st,要求从中删除任意的字符,求其满足aba的格式的最长字符串长度(左边和右边都是只有a的字符串,中间是只有b的字符串,当然也可以为空,不懂的话可根据样例理解一下
解题思路:当时想的是记录a和b的前缀和,然后暴力跑b的位置。(咩做出来。。。是的我又只做出来一题
赛后补题看到竟然可以用dp做,看一看代码,恍然大悟啊。
我们依然是dp的思想,对每一个元素进行分析:
如果是‘a’,那他要么是a串的,要么是c串的;
如果是a串的,直接a++;
如果是c串的,他有可能是b串后的第一个c,就是b+1,还有可能是依然接在c的后面,那就是c+1,取其中最长的就是当前最佳的c的长度
如果是‘b’,那他肯定是b串的,这时候和c一样,有两种情况,可能是a串后面的第一个b,就是a+1,还有可能是依然接在b的后面,就是b+1,取其中最长的就是当前最佳的b的长度
(这个特别像之前做的一道题,每一个元素要么是前一个串的后续,要么是后一个串的开头
ac代码:
1 #include <iostream>
2 using namespace std;
3 int main() {
4 string s;
5 cin>>s;
6 int a=0,b=0,c=0;
7 for(int i=0;i<s.size();++i) {
8 if(s[i]=='a') {
9 a++;
10 c=max(b+1,c+1);
11 }
12 else b=max(a+1,b+1);
13 }
14 cout<<max(b,c);
15 return 0;
16 }
codefforces 877B的更多相关文章
- codeforces 877b
B. Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- utf-8 汉字对照表
之前从redis中取出一些数据,utf8 16进制编码,想转成字符,没有找到现成的转化工具,先用这个表直接查找对照吧. UTF8编码表大全Code code# Code (coded in UTF-8 ...
- JS base64 加密和 后台 base64解密(防止中文乱码)
直接上代码 1,js(2个文件,网上找的) 不要觉的长,直接复制下来就OK //UnicodeAnsi.js文件 //把Unicode转成Ansi和把Ansi转换成Unicode function ...
- 基于nodejs实现js后端化处理
今天D哥给我提了个问题,"用php执行过js没"?咋一听,没戏~~毕竟常规情况下,js是依赖浏览器运行的.想在php后端采集的同时利用js运行结果并传递给php使用,没戏! 然后回 ...
- utf8汉字编码16进制对照
utf8汉字编码16进制对照 GB Unicode UTF-8 Chinese Character Code code# Code (coded in UT ...
- Html5模拟通讯录人员排序(sen.js)
// JavaScript Document var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_2 = & ...
- Base64 JAVA后台编码与JS前台解码(解决中文乱码问题)
中文通过Java后台进行Base64编码后传到前台,通过JS进行Base64解码时会出现中文乱码的问题,被这个问题也是困扰了几天,使用jquery.base64.js只能转码非中文字符,经过搜集各种方 ...
- 将table导出为excel格式文件
html: <table cellpadding="0" cellspacing="0" class="data_table" id= ...
- GBK UTF-16 UTF-8 编码表
GBK UTF-16 UTF-8 ================== D2BB 4E00 E4 B8 80 一 B6A1 4E01 E4 B8 81 丁 C6DF 4E03 E4 ...
随机推荐
- PyTorch 于 JupyterLab 的环境准备
PyTorch 是目前主流的深度学习框架之一,而 JupyterLab 是基于 Web 的交互式笔记本环境.于 JupyterLab 我们可以边记笔记的同时.边执行 PyTorch 代码,便于自己学习 ...
- spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)
spring-3.1.2.cxf-3.1.3.mybaties.mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xu ...
- 1.2V升压到3V和3.3V的升压芯片
1.2V镍氢电池升压到3V和3.3V输出,1.2V升压3V,1.2V升压3.3V稳压输出供电的芯片. PW5100 是一款低静态电流.达效率. PFM 模式控制的同步升压变换器. PW5100 所需的 ...
- MATLAB中load和imread的读取方式区别
load是导入文件,一般从mat文件中,读取的是结构体imread是图像处理工具箱的库函数,处理图像比较方便,读取的是矩阵 1.之前将数组或者矩阵保存为一个mat格式的文件,在进行load命令读取时: ...
- 删除HDFS中指定的文件。
1 import java.text.SimpleDateFormat; 2 import java.util.Scanner; 3 4 import org.apache.hadoop.fs.Fil ...
- Pusher Channels Protocol | Pusher docs https://pusher.com/docs/channels/library_auth_reference/pusher-websockets-protocol
Pusher Channels Protocol | Pusher docs https://pusher.com/docs/channels/library_auth_reference/pushe ...
- How to Gracefully Close Channels
小结: 1. When a goroutine sends a value to a channel, we can view the goroutine releases the ownership ...
- 【练习】goroutine chan 通道 总结
1. fatal error: all goroutines are asleep - deadlock! 所有的协程都休眠了 - 死锁! package mainimport("fmt&q ...
- (四)Springboot以jar包方式启动、关闭、重启脚本
Springboot以jar包方式启动.关闭.重启脚本 1.启动 2.关闭 3.重启 4.脚本授权 SpringBoot: nohup java -jar zlkj-data-server-1.0-S ...
- Fastjson使用实例
Fastjson使用实例 一.FastJson使用范例 1.1FastJson三个核心类 1.2Maven依赖 1.3Scala API 1.3.1反序列化 1.3.2序列化 1.4Java API ...