B. Nikita and string
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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?

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.

【题意】:给你一个只含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】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  6. Codeforces Round #442 (Div. 2)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  7. Codeforces Round #442 Div.2 A B C D E

    A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...

  8. Codeforces Round #442 (Div. 2) B题【一道模拟题QAQ】

    B. Nikita and string One day Nikita found the string containing letters "a" and "b&qu ...

  9. 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 ...

随机推荐

  1. [bzoj4889] [Tjoi2017]不勤劳的图书管理员

    Description 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被 ...

  2. 【学习笔记】Learning OpenCV3——Ch8 working with video

    Reading Video with the cv::VideoCapture Object 对象创建的三种方法: // 1. Input filename cv::VideoCapture::Vid ...

  3. 洛谷P3763 [Tjoi2017]DNA 【后缀数组】

    题目链接 洛谷P3763 题解 后缀数组裸题 在BZOJ被卡常到哭QAQ #include<algorithm> #include<iostream> #include< ...

  4. CF763B Timofey and Rectangles

    题目戳这里. 首先答案肯定是YES,因为一个平面图肯定可以被4种颜色染色,关键是怎么输出方案. 由于4是一个特殊的数字\(4 = 2^2\),而我们还有一个条件就是边长为奇数,而奇数是会改变二进制位的 ...

  5. 一些比较高效的CSS写法建议

    当浏览器解析html的时候,它构造了一个文档树来展现所有被显示的元素. 它在特定的样式表中去匹配元素,根据标准的css的层叠,继承和顺序规则, 在mozilla的实现中(可能其他的也是这样),对于每一 ...

  6. [hdu 6069]素数筛+区间质因数分解

    给[L,R]区间的每一个数都质因数分解的复杂度可以达到(R-L)logR,真的涨姿势…… 另外,质因数分解有很重要的一点,就是只需要打sqrt(R)以内的素数表就够了……因为超过sqrt(R)的至多只 ...

  7. POJ3436:ACM Computer Factory(最大流)

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9963   Accepted: 3 ...

  8. HDU 多校对抗赛 B Balanced Sequence

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. bzoj 2426 【HAOI2010】工程选址 贪心

    [HAOI2010]工厂选址 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 447  Solved: 308[Submit][Status][Disc ...

  10. video视频在结束之后回到初始状态

    目前尝试了两种解决方案,但是方案1在安卓移动端无法生效(猜测是因为移动端安卓启动的是原生的视频播放控件的原因) 方案一: 重新load资源,这种方法比较简洁,但是在安卓下不适用 video.addEv ...