Codeforces Round #442 (Div. 2) B题【一道模拟题QAQ】
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】的更多相关文章
- Codeforces Round #575 (Div. 3) 昨天的div3 补题
Codeforces Round #575 (Div. 3) 这个div3打的太差了,心态都崩了. B. Odd Sum Segments B 题我就想了很久,这个题目我是找的奇数的个数,因为奇数想分 ...
- Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)
Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Win10 自定义鼠标右键菜单
1. 点击文件鼠标右键显示软件 1.1 步骤 win+R输入regedit进入注册表 定位到HKEY_CLASSES_ROOT\*\shell下 在shell创建一个你想要的右键文件 例如:Kinok ...
- QT QcustomPlot的简单使用
第一步.QcustomPlot是QT提供的一个第三方库,在使用前需要在QcustomPlot官网上进行下载. 第二步.把解压完的QcustomPlot压缩包中的qcustomplot.h和qcusto ...
- Python print函数详解
1 """ 2 print(...) 3 print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=Fals ...
- GoF 的 23 种设计模式的分类和功能
1. 根据目的来分 根据模式是用来完成什么工作来划分,这种方式可分为创建型模式.结构型模式和行为型模式 3 种. 创建型模式:用于描述“怎样创建对象”,它的主要特点是“将对象的创建与使用分离”.GoF ...
- css — 选择器、盒子模型
目录 1. css引入方式 2. css选择器 3. css的盒模型 css: 层叠样式表 1. css引入方式 行内样式 <div style='color:red;'>mjj</ ...
- Struts2连接Mysql的Crud使用
今天分享的是struts2框架中增删改查的用法: 一:利用Struts2框架 1.1在pom.xml中导入相关依赖 <project xmlns="http://maven.apach ...
- 01 HTTP协议_servlet基础
一.定义 http(Hyper Text Transfer Protocol):超文本传输协议 二.作用 数据传输 三.概念 HTTP消息: 1.客户端发向服务器的请求消息 2.服务器回给客户端的响应 ...
- 判断pdf文件是否正常可用
之前在下载港交所的文件的时候会碰到有些文件异常的情况,文件下载下来,但是不能打开.查到其他的方法不是判断错误就是很麻烦. 整理出一个非常简单的方法,将文件以二进制方式打开,然后判断文件的开头是否符 ...
- access 数据库创建表SQL语法
create table R_CAIFA_B13 ( ID AUTOINCREMENT PRIMARY KEY, XB varchar(255), C1 varchar(50), C2 varchar ...
- 手机如何修改host文件
常常在想本地开发的时候使用pc端测试移动端,由于有安全域名限制,因此在本地修改了机器的host.将域名映射到本地局域网.这个做法是很正常的也很常见.但是现在新的需求是我需要在手机上也能够正常测试啊!怎 ...