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 ...
随机推荐
- COM组件(ActiveX)控件注册失败
这主要是由于旧版本的falsh player的原因,卸载干净并清理注册表,再次安装flash player即可. 具体方法: 1 在控制面板 卸载程序里面 卸载flash player 2 C:\WI ...
- canvas绘制直线和多边形基本操作
<!DOCTYPE HTML> <html lang="en"> <body> <canvas id="canvas" ...
- ANDROID FRAMENT的切换(解决REPLACE的低效)
http://www.cnblogs.com/android-joker/p/4414891.html 在项目中切换Fragment,一直都是用replace()方法来替换Fragment.但是这样做 ...
- myeclipse如何导入相应的jar包
方法一: 相应的项目------>右键------->build path-------->Config BuildPath...------->Libraries------ ...
- Drupal 7.31 SQL注入漏洞利用具体解释及EXP
有意迟几天放出来这篇文章以及程序,只是看样子Drupal的这个洞没有引起多少重视,所以我也没有必要按着不发了,只是说实话这个洞威力挺大的.当然.这也是Drupal本身没有意料到的. 0x00 首 ...
- Android编程之仿微信显示更多文字的View
微信朋友圈中,如果好友发表的文字过长,会自动收缩起来,底下有提示,当点击“显示更多”时才会展开. 首先定义布局文件(很简单,不解释): <?xml version="1.0" ...
- OpenGL 4.0 GLSL 实现 投影纹理映射(Projective Texture Mapping) (转)
http://blog.csdn.net/zhuyingqingfen/article/details/19331721 分类: GLSL 投影纹理映射 (projective texture ...
- SQL Server数据库大型应用解决方案总结
随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的互联网应用,每天百万级甚至上亿的PV无疑对数据库造成了相当高的负载.对于系统的稳定性和扩展性造成了极大的问题. 一 ...
- 两点C#的propertyGrid的使用心得
最近接触C#的PropertyGrid比较多,得到了两个小心得记录一下. 第1点是关于控制PropertyGrid中属性的只读属性的. 我遇到的问题是这样的,我需要在运行时根据SVN的状态动态控制Pr ...
- Android Compatibility package 兼容性开发套件
我们认为Android 3.0平板电脑操作系统在美国时间2011年2月22日的正式推出,对于Android手机应用程序开发者所象征的意涵是: 之前大家所开发过的Android手机应用,除了可以在And ...