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. php压缩文件夹并下载到本地

    /** * @param $path 要压缩的文件夹路径 * @param $filename 要生成的压缩包名称 */ public function create_zip($path,$filen ...

  2. Golang调用windows下的dll动态库中的函数 Golang 编译成 DLL 文件

    Golang调用windows下的dll动态库中的函数 package main import ( "fmt" "syscall" "time&quo ...

  3. UI自动化测试实战

    前言 前面我们已经搭建好了wordpress网站,如果需要查看运行效果可以看我前面的搭建文章,下面我们来进行自动化测试的练习. 示例 首先我们测试自动登陆 import unittest from s ...

  4. HTML5.1 新增的14项特性学习

    1.防止网络钓鱼攻击 使用target=_'blank'时, 新打开的标签可以更改window.opener.location到一些钓鱼网站,它会在开放页面上代表你执行一些Javascript代码.为 ...

  5. idea中将普通工程设置为maven项目

    只需要在工程上右键,"Add Frameworks support...",然后选择Maven即可

  6. .Net微服务实战之必须得面对的分布式问题

    系列文章 .Net微服务实战之技术选型篇 .Net微服务实战之技术架构分层篇 .Net微服务实战之DevOps篇 .Net微服务实战之负载均衡(上) .Net微服务实战之CI/CD .Net微服务实战 ...

  7. php之在admin的目录下的php文件里加上JSON的报头,运行php文件会提示下载

    去掉报头就正常,但在前端引用数据时要加上JSON.parse,不然读不出数据. $.get("fetchUpLast.php",{ rd:new Date().getTime()} ...

  8. C++的智能指针你了解吗?

  9. Linux lsblk和df命令区别

    lsblk 查看的是block device,也就是逻辑磁盘大小. df查看的是file system, 也就是文件系统层的磁盘大小

  10. 在VirtualBox上安装Ubuntu-20.04

    本文主要介绍如何在VirtualBox上安装Ubuntu-20.04 目录 下载VirtualBox 下载Ubuntu-20.04镜像 新建虚拟机 第一步:打开VirtualBox 第二步:设置虚拟机 ...