codeforces 676C C. Vasya and String(二分)
题目链接:
1 second
256 megabytes
standard input
standard output
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotesbeauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.
4 2
abba
4
8 1
aabaabaa
5 题意: 问最多改变k个字母才能使相同字母组成的子串最长; 思路: 最长的那个字串可以是a组成的,也可能是b组成的,现在枚举最长的子串的左端点,二分右端点,找到最长的长度,对于a,b两种情况都这样处理;用尺取法也可以搞; AC代码:
#include <bits/stdc++.h>
/*
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e5+;
int n,sum[N],k;
char s[N];
int check(int x,int y)
{
if(sum[x]-sum[y]<=k)return ;
return ;
}
int main()
{
scanf("%d%d",&n,&k);
scanf("%s",s);
sum[]=;
for(int i=;i<n;i++)
{
if(s[i]=='a')sum[i+]=sum[i];
else sum[i+]=sum[i]+;
}
int ans=;
for(int i=;i<=n;i++)
{
int l=i,r=n;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid,i-))l=mid+;
else r=mid-;
}
ans=max(ans,l-i);
}
for(int i=;i<n;i++)
{
if(s[i]=='b')sum[i+]=sum[i];
else sum[i+]=sum[i]+;
}
for(int i=;i<=n;i++)
{
int l=i,r=n;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid,i-))l=mid+;
else r=mid-;
}
ans=max(ans,l-i);
}
cout<<ans<<"\n";
return ;
}
codeforces 676C C. Vasya and String(二分)的更多相关文章
- Codeforces Round #354 (Div. 2) C. Vasya and String 二分
C. Vasya and String 题目连接: http://www.codeforces.com/contest/676/problem/C Description High school st ...
- codeforces 354 div2 C Vasya and String 前缀和
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #354 (Div. 2)——C. Vasya and String(尺取)
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Vasya and String(尺取法)
Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- C. Vasya and String
原题链接 C. Vasya and String High school student Vasya got a string of length n as a birthday present. T ...
- codeforces 676C
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 676C Vasya and String(尺取法)
题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...
- Codeforces 1073C:Vasya and Robot(二分)
C. Vasya and Robot time limit per test: 1 secondmemory limit per test: 256 megabytesinput: standard ...
随机推荐
- DateTable与List<T>相互转换 及JSON与DataTable(DataSet)相互转化
http://www.360doc.com/content/13/0712/09/10504424_299336674.shtml Linq处理List数据 http://blog.163.com/l ...
- 基本的TCP编程
int socket(int family,int type,int protocol); family: AF_INET ipv4协议 AF_INET6 ipv6协议 AF_LOCAL unix域协 ...
- xmlBean学习一
在文档中看到了xmlBean的出现,因为项目使用JMS,模块之间通过xml文件传递数据,就学一下xmlBean,java中还提供了DOM,SAX来解析xm,但也是比较麻烦的:而xmlbean则将xml ...
- Extjs 实现输入数量,实时更改总价
// 总价 var totalNum = '0.00'; //总价初始值 var $total = new Ext.form.Label({ text: '消费金额 : ¥' + totalNum + ...
- LNMP环境下压力测试时的主要调试参数
LNMP环境下压力测试时的主要调试参数: 进行HTTP的压力测试时,很多时候会遇到很小的并发数,服务器就会出现不响应,或者连接超时,一般导致的原因有如下几点: 一.Nginx主要调试参数 主模块参数: ...
- 【转】struct和typedef struct
原文:http://www.cnblogs.com/qyaizs/articles/2039101.html 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用type ...
- jQuery对象入门级介绍
你是否曾经见过像 $(".cta").click(function(){})这样的JavaScrip代码?或许你还会思考下 $('#X') 是什么,如果看到这些你都觉得摸不着头脑 ...
- 12个非常不错的免费HTML后台管理模板
下面介绍的这些免费后端管理HTML模板,都非常不错.建议您收藏. 1. Charisma Admin Template (示例) Charisma是一个响应式管理模板,基于Twitter Boots ...
- VMWare虚拟机网络的三种工作模式
VMWare提供了三种工作模式: 1.bridged(桥接模式) 在这种模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机,它可以访问网内任何一台机器.在桥接模式下,需要手工为虚拟系 ...
- SQL Server 2008 清空删除日志文件
USE [master]GOALTER DATABASE STAR9SQL SET RECOVERY SIMPLE WITH NO_WAITGOALTER DATABASE STAR9SQL SET ...