首页 技术 正文
技术 2022年11月8日
0 收藏 344 点赞 1,309 浏览 2011 个字

B. Nikita and stringtime 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.

Examplesinput

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289