codeforces Round 442 B Nikita and string【前缀和+暴力枚举分界点/线性DP】
2 seconds
256 megabytes
standard input
standard 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?
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Print a single integer — the maximum possible size of beautiful string Nikita can get.
abba
4
bab
2
It the first sample the string is already beautiful.
In the second sample he needs to delete one of "b" to make it beautiful.
【题意】:给你一个只含a,b的字符串,可以不改顺序地删去(也可以不删)一些字符使得其变为beautiful string。求能得到的最长beautiful string。beautiful string定义:字符串被分成三段(可以有空),不改变顺序,第一段和第三段只含有a,第二段只含有b。如:aa...aa/bb...bb/aa...aa 。
【分析】:
1.sa[i]表示[0,i)区间中a的个数,相当于前缀和,然后你遍历i,j把区间分成[0,i)[i,j)[j,n)三部分,求最大值即可
2.A[l]+A[i]-A[j]+B[j]-B[i],l代表字符串长度,直接预处理,预处理一下前i位和后i位有多少个a,然后直接枚举划分的位置
3.sa[i]表示 前i项有多少个a,sb[i]表示 前i项有多少个b,然后暴力n方可以枚举两个边界
取第一段[1,i]中所有的a,取第二段(i,j]中所有的b,取第三段(j,n]中所有的a,sa[i]代表[1,i],sa[i]-sa[j]就是[1,i]-[1,j]就是(j,i]
4.O(n)思路相当于直接dp
a表示当前只有第一段时的最大长度
ab表示当前有两段时的最大长度
aba表示有三段时的最大长度,也就是当前的答案
【代码】:
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll; const int mn=5e3+;
int n;
char s[mn];
int sa[mn],sb[mn]; int main() {
scanf("%s",s+);
n=strlen(s+);
for(int i=; i<=n+; i++) {
sa[i]=sa[i-]+(s[i]=='a');//前i项多少个a
sb[i]=sb[i-]+(s[i]=='b');
}
int ans=;
for(int i=; i<=n+; i++) {
for(int j=i; j<=n+; j++) {
ans=max(ans,sa[i]+sb[j]-sb[i]+sa[n]-sa[j]);
}
}
printf("%d\n",ans);
return ;
}
O(n^2)枚举
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
std::ios::sync_with_stdio(false);
string s;
cin>>s;
int n=s.length(),i;
int a=,aba=,ab=;
for(i=;i<n;i++)
{
if(s[i]=='a') aba++,a++;
if(s[i]=='b') ab++;
aba=max(aba,ab);
ab=max(ab,a);
}
cout<<aba;
return ;
}
O(n)dp
codeforces Round 442 B Nikita and string【前缀和+暴力枚举分界点/线性DP】的更多相关文章
- Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)
A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #447 (Div. 2) A. QAQ【三重暴力枚举】
A. QAQ time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- Codeforces Round #166 (Div. 2) A. Beautiful Year【暴力枚举/逆向思维/大于当前数且每个位数不同】
A. Beautiful Year time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #325 (Div. 2) A. Alena's Schedule 暴力枚举 字符串
A. Alena's Schedule time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- Codeforces Round #442 (Div. 2)
A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #442 Div.2 A B C D E
A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...
- Codeforces Round #442 (Div. 2) B题【一道模拟题QAQ】
B. Nikita and string One day Nikita found the string containing letters "a" and "b&qu ...
- Codeforces Round #297 (Div. 2)D. Arthur and Walls 暴力搜索
Codeforces Round #297 (Div. 2)D. Arthur and Walls Time Limit: 2 Sec Memory Limit: 512 MBSubmit: xxx ...
随机推荐
- Jsp上传组件Smartupload介绍
<form action="UploadServlet" enctype="multipart/form-data" method="post& ...
- Ubuntu下安装LNMP之nginx的卸载
我在安装Nginx时,是采用自己从网上down下自己需要的nginx版本进行编译安装的,如果使用过apt库来进行安装的话可以参考这篇文章:ubuntu中彻底删除nginx 假如是编译安装的童鞋,可以按 ...
- SLF4J 与Log4J
为什么要使用SLF4J而不是Log4J 每一个Java程序员都知道日志对于任何一个Java应用程序,尤其是服务端程序是至关重要的,而很多程序员也已经熟悉各种不同的日志库如java.util.loggi ...
- Linux下只允许用户远程scp
本文将介绍在Linux环境下,让用户不能远程登录 只能使用scp命令 使用到的软件:rssh(http://pizzashack.org/rssh/index.shtml ) 环境:centos6.x ...
- 通过js修改微信内置浏览器title
document.setTitle = function(t) { document.title = t; var i = document.createElement('iframe'); i.sr ...
- 如何去掉Json字符串中反斜杠
做项目的时候,遇到了这样的问题,前台传来的Json字符串在实体类中不对应(无法转换为实体类),而且传来的数据项是跟着数据库中的表的变动而变动的(不能重写实体类). 前台Json字符串为: string ...
- bzoj 5028: 小Z的加油店——带修改的区间gcd
Description 小Z经营一家加油店.小Z加油的方式非常奇怪.他有一排瓶子,每个瓶子有一个容量vi.每次别人来加油,他会让 别人选连续一段的瓶子.他可以用这些瓶子装汽油,但他只有三种操作: 1. ...
- AtCoder Regular Contest 082 F
Problem Statement We have a sandglass consisting of two bulbs, bulb A and bulb B. These bulbs contai ...
- SSM初步整合一
SSM(spring+springmvc+mybatis)初步整合一配置步骤: 步骤一.导入所需jar包 步骤二 .配置web.xml文件 <?xml version="1.0&quo ...
- 矩阵快速幂&T1
T1 知识储备 在写这一题之前,我们首先要了解矩阵乘法(我就是因为不懂弄了好久...) 矩阵的运算()-----(信息学奥赛一本通之提高篇) 矩阵的加法减法是十分简单的,就是把2个矩阵上对应的位置相加 ...