codeforces 676C
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 denotes beauty 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
In the first sample, Vasya can obtain both strings "aaaa" and "bbbb".
In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
题意:
给定一个只含字母a和b的字符串,你能够挑出<=k个字母来,让他变化成你想要的字母,问,在此变换下能够得到的最长的连续相同子串的长度是多少?
分析:
分两种情况,要连续a和连续b。
连续a 时,记录前面有多少个b字母,在这个上面进行尺取就行了。
#include <bits/stdc++.h> using namespace std; const int maxn = ;
char str[maxn];
int numb[maxn];
int numa[maxn]; int main()
{
//freopen("in.txt","r",stdin);
int n,k;
scanf("%d%d",&n,&k);
scanf("%s",str+); for(int i = ; i <= n; i++) {
if(str[i]=='b')
numb[i] = numb[i-] + ;
else numb[i] = numb[i-]; if(str[i]=='a')
numa[i] = numa[i-] + ;
else numa[i] = numa[i-];
} int L = ;
int R = ;
int ans = ;
int tmp = ;
int pos = ;
while(R<=n) {
while(R<=n&&numb[R]-tmp<=k) {
ans = max(ans,R-pos);
R++;
}
pos++;
tmp=numb[L];
L++;
} L = ;R = ;
tmp = ;
pos = ;
while(R<=n) {
while(R<=n&&numa[R]-tmp<=k) {
ans = max(ans,R-pos);
R++;
}
pos++;
tmp=numa[L];
L++;
} printf("%d\n",ans); return ;
}
codeforces 676C的更多相关文章
- Codeforces 676C Vasya and String(尺取法)
题目大概说给一个由a和b组成的字符串,最多能改变其中的k个字符,问通过改变能得到的最长连续且相同的字符串是多长. 用尺取法,改变成a和改变成b分别做一次:双指针i和j,j不停++,然后如果遇到需要改变 ...
- codeforces 676C C. Vasya and String(二分)
题目链接: C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces 划水
Codeforces 566F 题目大意:给定$N$个数,任意两个数之间若存在一个数为另一个数的因数,那么这两个数存在边,求图中最大团. 分析:求一个图最大团为NP-Hard问题,一般不采用硬方法算. ...
- 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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- unity接入讯飞教程
[全流程]<按照这个流程做即可,有不懂得可以看下面的2个><这个是<eclipse>> http://blog.csdn.net/qq_15267341/artic ...
- [转]批处理for命令使用指南
摘要:本文由浅入深,为大家专门讲解for的用法,希望大家喜欢. 首先应该明确的是,for不是一个简单的命令,它的用法比较复杂,它还可以带四个参数(/L /D /R /F),其中:/L和/F参数是最经常 ...
- pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- React.js 小书 Lesson15 - 实战分析:评论功能(二)
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson15 转载请注明出处,保留原文链接和作者信息. 上一节我们构建了基本的代码框架,现在开始完善其 ...
- Where Should an Architect Begin?--reference
http://www.bitnative.com/2014/01/24/where-should-a-software-architect-begin/ Where Should an Archite ...
- 2、按钮:Buttons
/* --- page1.html ---*/ <ion-content padding class="page1"> <h1>基本用法,实体框</h ...
- 网页生命周期-PageLoad事件
PageLoad事件的作用 l 页面载入是执行的处理命令 l 可以动态创建控件 l 可以设置现有控件的属性和状态 l 常用来设置数据库的连接 l 每次页面载入都会执行
- 在windows上用netsh动态配置端口转发
使用多个虚拟机,将开发环境和工作沟通环境分开(即时通,办公系统都只能在windows下使用…),将开发环境的服务提供给外部访问时,需要在主机上通过代理配置数据转发. VirtualBox提供了端口转发 ...
- c#中日期的处理
DateTime.Now.ToShortDateString()//只取日期DateTime.Now.ToLongTimeString();//只取时间搞定DateTime.Now.ToShortTi ...
- HashMap和Hashtable的比较
相同点 HashMap和Hashtable都是存储“键值对(key-value)”的散列表,而且都是采用拉链法解决hash冲突的.但是1.8中,hashmap引入了红黑树.Hashtable没有引入红 ...