B. Nikita and string

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.

Input

abba

Output


题意:给出一个字符串,这个字符串长度最大是5000,并且规定只有ab组成,现在问你,能不能将这个字符串分成三部分,第一和第三部分只有a或者是空,第二部分由b或者空组成。问你能不能分成这样的形式,如果能那么最长的长度是多少。

思路:暴力+模拟【只有5中情况。分别是a,b,ab,ba,aba。那么我们只要统计出这五种情况的最大的长度,最后我们取其中最大的就好了】

AC代码:{调了挺久的QAQ 菜是原罪}

 #include<bits/stdc++.h>

 using namespace std;
#define N 1200000
set<char> sss;
struct str{
int type;
int num;
int qian;// 前缀
int hou;// 后缀
}st[N];
int main(){
string str;
cin>>str;
int suma=;
int sumb=;
int cnt=;
int add=;
for(int i=;i<=str.size();i++){
if(str[i]=='a'&&i!=str.size()){
suma++;
}else if(str[i]=='b'&&i!=str.size()){
sumb++;
}
if(str[i]!=str[i+]){
st[cnt].type=str[i]-'a';
st[cnt++].num=add;
add=;
}else{
add++;
}
if(i!=str.size())
sss.insert(str[i]);
}
if(sss.size()==){ // a,b情况
printf("%d\n",str.size());
return ;
}
int sa=;
int sb=;
for(int i=;i<cnt;i++){// 前缀
if(st[i].type==){
st[i].qian=sa;
}else{
st[i].qian=sb;
}
if(st[i].type==){ // 类型:B
sb+=st[i].num;
}else{
sa+=st[i].num;
}
}
sa=;
sb=;
for(int i=cnt-;i>=;i--){
if(st[i].type==){
st[i].hou=sa;
}else{
st[i].hou=sb;
}
if(st[i].type==){ // 类型:B
sb+=st[i].num;
}else{
sa+=st[i].num;
}
}
/*
for(int i=0;i<cnt;i++){
printf("%d ",st[i].num);
}
printf("\n");
for(int i=0;i<cnt;i++){
printf("%d %d \n",st[i].qian,st[i].hou);
}
*/
int ans=max(suma,sumb); for(int i=;i<cnt;i++){
if(st[i].type==){
ans=max(ans,st[i].num+st[i].hou+st[i].qian);
int add=;
for(int j=i+;j<cnt;j+=){
add+=st[j].num;
ans=max(ans,st[i].qian+add+st[i].num+st[j].hou);
}
} }
for(int i=;i<cnt;i++){
ans=max(ans,st[i].num+st[i].hou);
int res=;
for(int j=i+;j<cnt;j+=){
res+=st[j].num;
ans=max(ans,st[i].num+res+st[j].hou);
}
} cout<<ans;
return ;
} /*
bbabbbbbaaba
aabaaaaabbab
aaaabaabbbbbaaabaaaa
a,b,ab,ba,aba bb a bbbbb aa b a 2 1 5 2 1 1 */

Codeforces Round #442 (Div. 2) B题【一道模拟题QAQ】的更多相关文章

  1. Codeforces Round #575 (Div. 3) 昨天的div3 补题

    Codeforces Round #575 (Div. 3) 这个div3打的太差了,心态都崩了. B. Odd Sum Segments B 题我就想了很久,这个题目我是找的奇数的个数,因为奇数想分 ...

  2. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

  3. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  4. Codeforces Round #334 (Div. 2) A. Uncowed Forces 水题

    A. Uncowed Forces Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/pro ...

  5. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  7. Codeforces Round #345 (Div. 2)——A. Joysticks(模拟+特判)

    A. Joysticks time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  8. Codeforces Round #306 (Div. 2) A. Two Substrings 水题

    A. Two Substrings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...

  9. Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题

    A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...

随机推荐

  1. Vue中常用知识点demo

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  2. Excel逻辑运算函数

    Excel逻辑运算函数 1.FALSE和TRUE的使用 ​ 筛选出表中salary>6.gender为男.age>28至少满足这三个条件中的两个的数据 1.依次使用:=C2>6.=D ...

  3. flask使用tablib导出excel数据表

    在网页中常常有导出数据的需求,尤其是一下管理类平台.在flask中要导出excel数据表,通常可以使用xlwt库,创建文件并逐行写入数据,但是使用起来总是感觉很麻烦.tablib库相对操作更加方便. ...

  4. 关于OI中的各种数学

    学到后面数学越来越多了,感觉好难啊,开个博客专门记录一下数学相关的东西 因为反正也没人看,所以主要还是给自己看的 一些符号: 数论函数的卷积:$\ast$,$ h = f \ast g$ 则 $h(n ...

  5. Comet OJ Contest #3

    A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...

  6. Python操作Redis(转)

    原文1:https://cloud.tencent.com/developer/article/1151834 原文2:https://www.cnblogs.com/zhaohuhu/p/91406 ...

  7. Neo4j基本使用及导入三元组

    下载和安装Neo4j 安装Java JDK 下载Neo4j安装文件 创建系统环境变量 Neo4j配置 配置文档存储在conf目录下,Neo4j通过配置文件neo4j.conf控制服务器的工作.默认情况 ...

  8. (错误)Lucene工具Luck启动错误

    启动luke命令行下图错误 错误原因:luke版本和lucene版本不匹配,lucene5.3.0版本必须用luke5.3.0版本才能打开索引. 解决方法:更新luke版本即可. luke 所有版本下 ...

  9. (五)Spring Boot之@RestController注解和ConfigurationProperties配置多个属性

    一.@RestController和@Controller的区别 @RestController注解相当于@ResponseBody + @Controller合在一起的作用. 如果只是使用@Rest ...

  10. (一)weblogic11g的安装配置

    一.安装 找到weblogic安装包,小编这里用的是wls1034_win32.exe版本,双击打开 完成后运行快速启动,打开快速启动界面,配置weblogic.如果没有打开,还可以在开始菜单中找到q ...