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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- ics httpDELETE 时增加 content,length 特别需求
unit: OverbyteIcsHttpProt.pasprocedure THttpCli.SendRequest(const Method, Version: String); var Head ...
- Yii2 执行Save()方法失败,却没有错误信息
一般用$model->errors 就能查看到更新失败的原因,但是这次却什么错误信息都没有,最后发现是因为在模型类中定义了一个方法 public function beforeSave($ins ...
- 转帖 最全的HTML、CSS知识点总结,浅显易懂
一,html+css基础1-1Html和CSS的关系学习web前端开发基础技术需要掌握:HTML.CSS.JavaScript语言.下面我们就来了解下这三门技术都是用来实现什么的:1. HTML是网页 ...
- NPOI开发手记
目录 注意事项 读取Excel 创建Excel表 保存Excel 行 列 单元格样式 添加公式 Dataset.DataGridView转换Excel帮助类 NPOI其实就是POI的.NET移植 项目 ...
- java中的各种修饰符作用范围
访问修饰符: private 缺省 protected public 作用范围: 访问修饰符\作用范围 所在类 同一包内其他类 其他包内子类 其他包内非子类 private 可以访问 不可以 不可以 ...
- springboot整合mongo多数据源
该实例已测试 POM.XML <!-- Spring Boot mongodb 依赖--> <dependency> <groupId>org.springfram ...
- [转]Session and application state in ASP.NET Core
本文转自:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state By Rick Anderson and Steve ...
- info.plist 安全登录
设置info.plist 安全登录 App Transport Security Settings dictionary Allow Arbitrary Loads Boolean YES
- [LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- 洛谷P1966 火柴排队(逆序对)
题意 题目链接 Sol 不算很难的一道题 首先要保证权值最小,不难想到一种贪心策略,即把两个序列中rank相同的数放到同一个位置 证明也比较trivial.假设\(A\)中有两个元素\(a, b\), ...