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 ...
随机推荐
- 免费的天气Web Service接口
免费的天气Web Service接口 在android应用当中很多时候需要获取天气的信息,这里提供怎么获取天气信息: 1. http://www.ayandy.com/Service.asmx?wsd ...
- Java NIO通信框架在电信领域的实践
[http://www.codeceo.com/article/java-nio-communication.html] 华为电信软件技术架构演进 Java NIO框架在技术变迁中起到的关键作用 ...
- MVC6与Asp.net5
http://www.cnblogs.com/n-pei/p/4263148.html https://blogs.msdn.microsoft.com/scottgu/2015/04/30/asp- ...
- 不需要JAVAScript完成分页查询功能
分页查询之前已经说过,现在用另一种方法实现,换汤不换药.但是更简单. view层代码: 控制层代码: 业务逻辑层,主要看一下方法count1()的代码: count1()方法的功能就是控制翻页,如果传 ...
- ref和out的使用与区别
out的使用 ————————————————————————————————————————————————— class Program { static void Main( ...
- Builder
Builder模式的使用情景 相同的方法, 不同的执行顺序, 产生不同的事件结果 多个部件或零件, 都可以装配到一个对象中, 但是产生的运行结果又不相同 产品类比较复杂, 或者产品类中的调用顺序不同产 ...
- java中导入常量
import关键字除了导入包之外,还可以导入静态成员,这时JDK5.0以上版本提供的新功能.导入静态成员可以是程序员编程更为方便. 使用import导入静态成员的语法为: import static ...
- DISCUZ X2更换域名注意事项
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 基于Andoird 4.2.2的同步框架源代码学习——同步发起端
关键组件: ContentResolver ContentService SyncManager SyncManager.ActiveSyncContext SyncManager.SyncOpera ...
- 【M18】分期摊还预期的计算成本
1.基本思想就是:如果将来肯定要做某件事,并且这件事情耗时,提前把东西准备好,先做一部分.常用的使用场景有: 2.考虑一个大的数据集合,集合中元素不断变化.经常要取出里面的最大值,正常的做法是:每次调 ...