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只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- DOM-设置样式心得
一.style属性的设置和获取 style是一个对象,不能通过内嵌或外链获取,也就是只有是行内式的时候才能打印显示 style本身是一个对象 属性的值是字符串,没有赋值的情况下是"" ...
- Andriod——手机尺寸相关的概念 +尺寸单位+关于颜色
手机的尺寸: 屏幕对角线的长度,单位为英寸(2.54cm) 手机的分辨率: 屏幕能显示的像素的数量, 一般用在长方向上数量*宽方向上数量来表达 手机的像素密度: pixels per inch,也称P ...
- Oracle数据库mybatis 插入空值时报错(with JdbcType OTHER)
参考文档: 1.https://blog.csdn.net/fishernemo/article/details/27649233 2.http://helgaxu.iteye.com/blog/21 ...
- BZOJ1791或洛谷4381 [IOI2008]Island
一道基环树的直径 BZOJ原题链接 洛谷原题链接 又是一道实现贼麻烦的题.. 显然公园其实是基环树森林,求的最长距离其实就是求每一棵基环树的直径的总和. 对于每棵基环树,其直径要么经过环,要么是某个环 ...
- BZOJ 1874 取石子游戏 - SG函数
Description $N$堆石子, $M$种取石子的方式, 最后取石子的人赢, 问先手是否必胜 $A_i <= 1000$,$ B_i <= 10$ Solution 由于数据很小, ...
- Ubuntu几种常见乱码解决方法
一.网页中的flash乱码: ubuntu默认浏览器是Firefox,但是Ubuntu默认不安装像flash这种带版权的软件,所以当你浏览像youku或网页播放器时,这种带有 flash ...
- maven项目springmvc导包失败-JsonProcessingException
昨天搞了好久还是没有解决,今天把springaop小demo敲一遍时,启动tomcat时报错: org.springframework.beans.factory.BeanCreationExcept ...
- Mac OS X Git安装教程
http://code.google.com/p/git-osx-installer上也提供了一个Git的图形化客户端:OpenInGitGui,可以从这里获得,OpenInGitGui十分小巧,下载 ...
- python3版本main.py执行产生中间__pycache__详解
__pycache__ 用python编写好一个工程,在第一次运行后,总会发现工程根目录下生成了一个__pycache__文件夹,里面是和py文件同名的各种 *.pyc 或者 *.pyo 文件. 先大 ...
- 【转】CentOS 7 安装配置 NFS
环境 nps 192.168.1.97 client 192.168.1.98 一.yum 安装 yum -y install nfs-utils rpcbind nfs 的配置文件 /etc/exp ...