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只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- CSharp陷阱1
CSharp陷阱1 环境:sharpdevelop 3 .net 2.0 正确的 internal static readonly string[] string_2 = new ...
- Pycharm的常用快捷将
程序运行 Shift+alt+F10 执行程序 debug调试 Shift+alt+F9 Debug调试Shift + F9 对当前文件进行DebugF8 调试模式下 跳过F7 调试模式下 进入F9 ...
- RAC初步使用
信号基本流程: //1:创建信号 RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSub ...
- SPring中quartz的配置(可以用实现邮件定时发送,任务定时执行,网站定时更新等)
http://www.cnblogs.com/kay/archive/2007/11/02/947372.html 邮件或任务多次发送或执行的问题: 1.<property name=" ...
- mysql 设置用户并授权
一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...
- 【统一异常处理】@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
1.利用springmvc注解对Controller层异常全局处理 对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service ...
- spring 的 ApplicationContext.getBean(type) 无法获取bean,报错
具体问题请看 https://q.cnblogs.com/q/108101/ 研究了两天: 经过上文中的排除法: 造成问题的原因是要获取的bean 中 有被切入的方法.. 就是可能该类会使用反射生 ...
- Git及其二次开发
Git And TFS: 将 Visual Studio 用于 Git:http://msdn.microsoft.com/zh-cn/library/hh850437.aspx TFVC 和 Git ...
- centos上安装jdk
今天在centos上安装jdk,总结步骤如下:1.先到oracle下载rpm包:jdk-7u80-linux-x64.rpm (下载地址在百度找找)2.把jdk-7u80-linux-x64.rpm上 ...
- 在myeclipse上发布自己的webservice
什么是WebServices? 它是一种构建应用程序的普遍模型,可以在任何支持网络通信的操作系统中实施运行;它是一种新的web应用程序分支,是自包含.自描述.模块化的应用,可以发布.定位.通过w ...