codeforces 877b
2 seconds
256 megabytes
standard input
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?
The first line contains a non-empty string of length not greater than 5 000 containing only lowercase English letters "a" and "b".
Print a single integer — the maximum possible size of beautiful string Nikita can get.
abba
4
bab
2
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 存在arr[5005][2]里面 arr[i][0]表示i后面包括i有arr[i][0]个a arr[i][1]表示i后面包括i有arr[i][1]个b
遍历每个段位
for(i =0 ; i < strlen(str);++i)
{
if(str[j] == 'b')
{
for(j = i; j < strlen(str);++j)
{
if(str[j] == 'b')
maxn = max(maxn, arr[0][0] - arr[i][0] + arr[i][2] - arr[j][2] + 1 + arr[j][0]);
}
}
}
注意全是a的情况
丑陋的代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
int main()
{
// char str[5010];
string str;
int arr[5010][2];
int i,j,k,a,b,a1,b1;
// memset(str,0,sizeof(str));
int maxn = 0;
a1 = b1 = a = b = 0;
// scanf("%s",str);
cin >> str;
memset(arr, 0, sizeof(arr));
for(i = 0; i < str.length(); ++i)
{
if(str[i] == 'a')
a++;
else
b++;
}
if(b == 0)
{
printf("%d\n",(int)str.length());
return 0;
}
a1 = b1 = 0;
for(i = 0; i < str.length(); ++i)
{
if(str[i] == 'a')
{
arr[i][0] = a - a1;
arr[i][1] = b - b1;
a1++;
}
else
{
arr[i][0] = a - a1;
arr[i][1] = b - b1;
b1++;
}
}
a1 = b1 = 0;
for(i = 0; i < str.length(); ++i)
{
if(str[i] == 'b')
{
for(j = i; j < str.length(); ++j)
{
if(str[j] == 'b')
maxn = max(maxn,a - arr[i][0] + arr[i][1] - arr[j][1] + arr[j][0]+1);
}
}
}
printf("%d\n",maxn);
}
codeforces 877b的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- Liunx 重定向,管道符(转)
原作网址:http://blog.csdn.net/qq_16811963/article/details/52997178 输出重定向 >代表以覆盖的方式将命令的正确输出输出到指定的文件或设备 ...
- BPF+XDP比较全的资料都在这里
Dive into BPF: a list of reading material Sep 1, 2016 • Quentin Monnet◀Table of contents What is BPF ...
- SVN查看最新几次提交日志的命令
Windows下,使用svn客户端查看日志很方便,linux下查看的记录条数的小技巧: 1>查看某个目录下的所有日志记录 svn log [PATH] 上面的方式,对于命令行下查看时是很不方便的 ...
- java中 this 关键字的三种用法
Java中this的三种用法 调用属性 (1)this可以调用本类中的任何成员变量 调用方法(可省略) (2)this调用本类中的成员方法(在main方法里面没有办法通过this调用) 调用构造方法 ...
- HashMap 、HashTable、TreeMap、WeakHashMap的区别是什么
Java为数据结构中的映射定义了一个接口java.util.Map,它有4个实现类:HashTable.HashMap.TreeMap.WeakHashMap. HashMap和HashTable的区 ...
- Composer 中文镜像 Lavavel-china 公益项目
『Composer 中国全量镜像』是由 Laravel China 社区联合 又拍云 与 优帆远扬 共同合作推出的公益项目,旨在为广大 PHP 用户提供稳定和高速的 Composer 国内镜像服务. ...
- Python之路(第六篇)Python全局变量与局部变量、函数多层嵌套、函数递归
一.局部变量与全局变量 1.在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量.全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序. 全局变量没有任何缩进,在任何位置都可 ...
- SSRF-php初探
0x00 前言 1) SSRF的概念很好理解,请自行百度. 2) JAVA/PHP/PYTHON都存在SSRF漏洞(至于其他语言的情况,了解粗浅尚不得知). 3) SSRF的利用方式 ...
- Linux 禁止root 用户登录启用sudo
1.添加sudo用户执行visudo命令,找到: 复制代码 代码如下: root ALL=(ALL) ALL 在下面增加:(注意,qianyunlai 是Linux新增的一个普通用户),没有的话可创建 ...
- 用百度AI的OCR文字识别结合JAVA实现了图片的文字识别功能
第一步可定要获取百度的三个东西 要到百度AI网站(http://ai.baidu.com/)去注册 然后获得 -const APP_ID = '请填写你的appid'; -const API_KEY ...