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. sql语句编写 有时候一个子查询可以拆分成多个子查询

    sql语句编写 有时候一个子查询可以拆分成多个子查询

  2. Python 日期时间相关

    #calendar日历模块 import calendar #calendar() 获取一年日历 result = calendar.calendar(2018,w = 2,l = 1,c = 6,m ...

  3. [luogu P1442] 铁球落地

    题目链接 线段树\(+dp\). 先用线段树预处理出每个线段从左边和右边掉落到哪里,记为\(f[i][0/1]\). 然后记\(g[i][0/1]\)为到达第\(i\)个线段的左边或右边所要的最小时间 ...

  4. Windows关机过程分析与快速关机

    原文链接:http://blog.csdn.net/flyoxs/article/details/3710367 Windows开机和关机慢,很多时候慢得令人抓狂.特别是做嵌入式开发时(如XPE和Wi ...

  5. Tomcat-Jdbc-Pool连接池参数说明

    原文  http://liuxing.info/2016/01/05/Tomcat-Jdbc-Pool参数说明/ 转载收藏用,如有侵权,请联系删除,谢谢. 介绍 Tomcat 在 7.0 以前的版本都 ...

  6. JavaScript的相等(==)与全等(===)

    有段代码如下: view source   print? 1 if (![] == []) { 2     //Code 3 } ![] == [],true or false? 我们都知道,ECMA ...

  7. POJ1797:Heavy Transportation(改造Dijkstra)

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 52728   Accepted:  ...

  8. jsonp解析 html

    https://jsoup.org/cookbook/  官网的教程, 很详细! <dependency> <groupId>org.jsoup</groupId> ...

  9. 2、Distributed Optimization

    一.目录: Distributed dynamic programming (as applied to path-planning problems). Distributed solutions ...

  10. 【Mysql优化】key和index区别

    mysql的key和index多少有点令人迷惑,这实际上考察对数据库体系结构的了解的.    1).key 是数据库的物理结构,它包含两层意义,一是约束(偏重于约束和规范数据库的结构完整性),二是索引 ...