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 ...
随机推荐
- Centos7.3安装nexus12.1
nexus.12.1-01的安装 1.下载nexus 2.上传到服务器/root/ 3.解压 t ...
- golang goroutine进行通信 channel
1.channel的读取与声明 //goroutine之间利用channel进行通信 package main import ( "fmt" "time" ) ...
- 2019java学习路线图
学习路线图往往是学习一样技术的入门指南.网上搜到的Java学习路线图也是一抓一大把.但是很多学习路线图总结的云里雾里,也没有配套的视频,学习效果并不好. 分享一个完整的Java学习路线图给大家,也是贴 ...
- go 实现每次生成不同随机值
直接使用rand.Intn(10) 多次运行发现每次的随机值都是一样的 查看 Intn方法的源码说明 // Intn returns, as an int, a non-negative pseudo ...
- redis mongodb持久化的方式
目录 redis持久化方式(两种) RDB持久化 AOF持久化 两种持续化方式需要明确的问题 对比 MongoDB持久化方式 redis持久化方式(两种) RDB持久化 redis提供了RDB持久化的 ...
- AtCoder Grand Contest 040 B - Two Contests
传送门 一看就感觉很贪心 考虑左端点最右的区间 $p$ 和右端点最左的区间 $q$ 如果 $p,q$ 属于同一个集合(设为 $S$,另一个集合设为 $T$),那么其他的区间不管是不是在 $S$ 都不会 ...
- (十四)SpringBoot之事务处理
一.简介 ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作: 用来保证一致性,即service方法里的多个dao操作 ...
- Quartz.net任务调度(石英钟定时任务)
好了,现在具体来说一下怎么使用Quartz.net 2.0. 1.到网上下载Quartz.net 2.0,下载完后解压,里面有vs.net2008和vs.net2010两个版本. 2.新建一个空项目, ...
- C#项目中窗体的ShowDialog()和show()的区别
ShowDialog()弹出的窗体为模式化窗体: show()弹出的窗体为非模式化窗体: 模式化窗体与非模式化窗体的区别: 模式化窗体会使程序中断,直到关闭窗体: 打开窗体后不能替换到其他窗体: 子窗 ...
- mysql8中查询语句表别名不能使用 “of”
今天在迁移一个项目的时候,发现有一个sql报错,但是语句跟迁移之前完全一样,所以想来应该是 mysql 版本差异导致的. 迁移之前版本:5.6.28(腾讯云) 迁移之后版本:8.0.16(阿里云) 新 ...