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的更多相关文章

  1. codeforces 877b

    B. Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. utf-8 汉字对照表

    之前从redis中取出一些数据,utf8 16进制编码,想转成字符,没有找到现成的转化工具,先用这个表直接查找对照吧. UTF8编码表大全Code code# Code (coded in UTF-8 ...

  3. JS base64 加密和 后台 base64解密(防止中文乱码)

    直接上代码 1,js(2个文件,网上找的)  不要觉的长,直接复制下来就OK //UnicodeAnsi.js文件 //把Unicode转成Ansi和把Ansi转换成Unicode function ...

  4. 基于nodejs实现js后端化处理

    今天D哥给我提了个问题,"用php执行过js没"?咋一听,没戏~~毕竟常规情况下,js是依赖浏览器运行的.想在php后端采集的同时利用js运行结果并传递给php使用,没戏! 然后回 ...

  5. utf8汉字编码16进制对照

           utf8汉字编码16进制对照  GB    Unicode  UTF-8     Chinese Character Code  code# Code      (coded in UT ...

  6. Html5模拟通讯录人员排序(sen.js)

    // JavaScript Document var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_2 = & ...

  7. Base64 JAVA后台编码与JS前台解码(解决中文乱码问题)

    中文通过Java后台进行Base64编码后传到前台,通过JS进行Base64解码时会出现中文乱码的问题,被这个问题也是困扰了几天,使用jquery.base64.js只能转码非中文字符,经过搜集各种方 ...

  8. 将table导出为excel格式文件

    html: <table cellpadding="0" cellspacing="0" class="data_table" id= ...

  9. GBK UTF-16 UTF-8 编码表

    GBK   UTF-16 UTF-8 ================== D2BB  4E00  E4 B8 80  一 B6A1  4E01  E4 B8 81  丁 C6DF  4E03  E4 ...

随机推荐

  1. 前端知识(一)05 axios-谷粒学院

    目录 一.axios的作用 二.axios实例 1.复制js资源 2.创建 axios.html 3.引入js 4.启动课程中心微服务 5.编写js 6.html渲染数据 7.跨域 8.使用生命周期函 ...

  2. CoeMonkey少儿编程第4章 变量

    点击这里,现在就开启CodeMonkey的趣味编程之旅. 目标 了解什么是变量 了解变量的命名规则 掌握如何使用变量 变量 什么是变量?顾名思义,变量就是可以变化的量. 和变量相对的是常量,即不可变化 ...

  3. 转 6 jmeter元件的作用域与执行顺序

    6 jmeter元件的作用域与执行顺序   元件的作用域 配置元件(config elements)会影响其作用范围内的所有元件.前置处理程序(Per-processors)在其作用范围内的每一个sa ...

  4. (06)-Python3之--判断、循环

    1.判断(if) 语法: if 条件(True/False): 条件为真时,执行的代码(要干的事情)[elif 条件: 条件为真时,执行的代码(要干的事情)elif 条件: 条件为真时,执行的代码(要 ...

  5. jQuery 实现复制功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 自导自演的面试现场,趣学MySQL的10种文件

    导读 Hi,大家好!我是白日梦!本文是MySQL专题的第 24 篇. 今天我要跟你分享的MySQL话题是:"自导自演的数据库面试现场--谈谈MySQL的10种文件" 换一种写作风格 ...

  7. 三路握手 三报文握手 TIME_WAIT three way handshake three message handshake

    UNIX网络编程卷1:套接字联网API(第3版) 第2章 传输层:TCP.UDP和SCTP 2.4 TCP TCP不能被描述成100%可靠的协议 数次重传失败,则放弃 数据的可靠递送和故障的可靠通知 ...

  8. # Functions are First-Class Citizens in Python 一等公民

    # Functions are First-Class Citizens in Python 一等公民https://cn.bing.com/search?form=MOZSBR&pc=MOZ ...

  9. ensure that both new and old access_token values are available within five minutes, so that third-party services are smoothly transitioned.

    WeChat public doc https://developers.weixin.qq.com/doc/offiaccount/en/Basic_Information/Get_access_t ...

  10. Language Guide (proto3) | proto3 语言指南(九)Oneof结构

    Oneof - Oneof结构 如果消息包含多个字段,并且最多只能同时设置一个字段,则可以使用oneof功能强制执行此行为并节省内存. oneof字段与常规字段类似,但oneof共享内存中的所有字段除 ...