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. python学习-22 字符串格式化

    格式化包括:百分号方式和format方式 1.百分号 - %s   (%.4s   表示截取了4个字符) 传单个值: 例如: print('i am %s sex boy is ljj'%123) 运 ...

  2. Django dumpdata and loaddata

    目录 dumpdata 命令 dumpdata 基本数据库的转存 dumpdata 备份特定的 app dumpdata 备份特定的表 dumpdata (--exclude) dumpdata (- ...

  3. Java CountingSort

    Java CountingSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternation ...

  4. 怎样用sql语句复制表table1到表table2的同时复制主键

    原文:怎样用sql语句复制表table1到表table2的同时复制主键 在从table1表复制到table2的时候,我们会用语句: select * into table2 from table1 但 ...

  5. Snort Inline IPS Mode

    Snort Inline IPS Mode https://forum.netgate.com/topic/143812/snort-package-4-0-inline-ips-mode-intro ...

  6. QT实现两条贪吃蛇

    Snake.pro文件 1 #------------------------------------------------- 2 # 3 # Project created by QtCreato ...

  7. cmd查找端口占用情况

    查找端口占用情况:netstat -ano|findstr 4848 查看使用指定端口的应用程序:tasklist|findstr xxxx,xxxx指的是pid 结束指定进程:taskkill /p ...

  8. SpringCloud"灰度部署"——动态刷新网关配置

    通过Acutator和SpringCloudConfig完成"灰度部署"——动态刷新网关路由配置 先声明下,我这个可能是冒牌的灰度部署,技术有限,纯粹个人笔记分享. 前段时间接到了 ...

  9. 什么是实体关系图(ERD)? 转

    https://www.visual-paradigm.com/cn/guide/data-modeling/what-is-entity-relationship-diagram/#erd-data ...

  10. 阮一峰:jQuery官方基础教程笔记

    jQuery是目前使用最广泛的javascript函数库. 据统计,全世界排名前100万的网站,有46%使用jQuery,远远超过其他库.微软公司甚至把jQuery作为他们的官方库. 对于网页开发者来 ...