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. logicaldisk本地磁盘管理

    在网上搜了很多,但是基本都是一样的,差不多都是互相转载摘抄,就那么几个寥寥无几的例子,所以我冒了很大的风险,自己经过多次的测试,对这个命令有了一些新的认识!拿出来分享一下! LOGICALDISK   ...

  2. wmic process进程管理

    process    进程管理工具 示例:1.列举当前的进程.进程路径.命令行.进程ID.父进程ID.线程数,内存使用::wmic process get name,executablepath,co ...

  3. Docker相关简介以及使用方法

    Docker: 可以把它看作是一个软件,在这个软件当中呢,还可以安装其他的软件,还可以把软件所需要的环境依赖一起添加进来,这样让开发人员的程序在不同的环境当中都可以流转起来,避免了程序出现" ...

  4. JS中常用的工具类

    一.日期工具类 /** * 日期时间工具类 * @type {{dateFormat}} */ var DateTime = function () { var patterns = { PATTER ...

  5. Unix Socket 代理服务 unix域套接字

    基于Unix Socket的可靠Node.js HTTP代理实现(支持WebSocket协议) - royalrover - 博客园 https://www.cnblogs.com/accordion ...

  6. 加密填补 填充 pad padding

    RFC 1423 - Privacy Enhancement for Internet Electronic Mail: Part III: Algorithms, Modes, and Identi ...

  7. 为什么要使用 do while(0)?

    两点 避免宏定义的花括号对代码的完整性造成影响 可以在指定的代码块中(do{})使用break提前跳出,避免goto.

  8. eclipse项目放到github

    一,下载git ,配置用户名和邮箱: git config --global user.name "name"       git config --global user.ema ...

  9. SVN、GIT比较

    Git是分布式的,完全可以不备份代码,下载下来后,在本地不必联网就可以看到所有的log,跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并,也可以不用联网在本地提交 SVN ...

  10. Docker中运行nginx

    Docker中运行nginx 1.Docker中运行nginx 2.配置文件 2.1 nginx.conf 2.2 default.conf 3.docker的镜像可以挂什么卷 部分内容原文地址: C ...