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 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
随机推荐
- ElasticSearch:集群(Cluster),节点(Node),分片(Shard),Indices(索引),replicas(备份)之间关系
[Cluster]集群,一个ES集群由一个或多个节点(Node)组成,每个集群都有一个cluster name作为标识----------------------------------------- ...
- java多线程之线程组与线程池
看这篇文章:http://blog.csdn.net/zen99t/article/details/50909099
- Basic Data Structures and Algorithms in the Linux Kernel--reference
http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...
- oracle dblink简介
database link概述 database link是定义一个数据库到另一个数据库的路径的对象,database link允许你查询远程表及执行远程程序.在任何分布式环境里,database都是 ...
- 利用request、beautifulsoup、xml写多线程爬虫
# -*- coding:UTF-8 -*- import requests,time from collections import OrderedDict import threading fro ...
- Javascript模块化编程详解
在这篇文章中,我将会回顾一下js模块化编程的基础,并且将会讲到一些真的非常值得一提的进阶话题,包括一个我认为是我自创的模式. 模块化编程是一种非常常见Javascript编程模式.它一般来说可以使得代 ...
- 获取memcache中所有数据
remap_table方法是用的一个框架写的: $gvs = $this->pageObj->get;是获取通过get方式传递过来的数据: $mem = $this->pageObj ...
- vsCode代码缩略图
vsCode配置代码缩略图: 文件--首选项--设置 搜索 minimap true 打开 false 关闭
- Fragment中的方法findFragmentById(int id)的返回值探讨
在学习<Android编程权威指南>P124页的时候,遇到了这样的代码: 引起了我的疑问if的判断条件是(fragment==null),那执行完上一句 Fragment Fragment ...
- oracle学习篇四:多表查询
-----------------产生笛卡儿积------------------------------------ select * from emp,dept; --不带条件时,记录数为14*4 ...