题目链接:

http://codeforces.com/contest/676/problem/C

题解:

把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍。

一段值改变一部分的情况考虑的不够周到,wa了两次。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std; const int maxn = + ;
char str[maxn];
int arr[maxn], tot;
int sum[maxn]; int n, k; int solve(int fst) {
int ret = -, cnt = ;
int p1 = fst;
for (int p2 = fst; p2 <= tot; p2 += ) {
cnt += arr[p2];
while (p1 <= p2&&cnt > k) {
cnt -= arr[p1]; p1 += ;
}
if (p1 <= p2) {
if (p1 - >= && p2 + <= n) {
ret = max(ret, sum[p2 + ] - sum[p1 - ]+k-cnt);
ret = min(ret, n);
}
else if (p1 - >= ) {
ret = max(ret, sum[p2] - sum[p1 - ] + k - cnt);
ret = min(ret, n);
}
else if (p2 + <= n) {
ret = max(ret, sum[p2 + ] - sum[p1 - ] + k - cnt);
ret = min(ret, n);
}
else {
ret = max(ret, sum[p2] - sum[p1 - ] + k - cnt);
ret = min(ret, n);
}
}
}
return ret;
} int main() {
while (scanf("%d%d", &n, &k) == && n) {
scanf("%s", str);
tot = ;
int cnt = ;
for (int i = ; i < n - ; i++) {
if (str[i] != str[i + ]) {
arr[++tot] = cnt;
cnt = ;
}
else {
cnt++;
}
}
arr[++tot] = cnt;
sum[] = ;
for (int i = ; i <= tot; i++) sum[i] = sum[i - ] + arr[i];
int ans = ;
for (int i = ; i <= tot; i++) {
ans = max(ans, arr[i] + k);
ans = min(ans, n);
}
ans = max(ans, solve());
ans = max(ans, solve());
printf("%d\n", ans);
}
return ;
} /*
4 2
abba
8 1
aabaabaa
5 2
ababa
5 1
ababa
4 1
aaba
5 0
aaaaa
*/

Codeforces Round #354 (Div. 2) C. Vasya and String的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  5. 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root

    题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...

  6. Codeforces Round #184 (Div. 2) E. Playing with String(博弈)

    题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...

  7. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  8. 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String

    题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...

  9. Codeforces Round #354 (Div. 2)-C

    C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...

随机推荐

  1. Sqlserver数据库存储路径的修改

    Sqlserver数据库存储路径的修改 Sqlserver数据库存储路径问题:本系统sqlserver路径默认是存储在C盘目录下的,由于数据会慢慢变大和避免重装系统数据丢失等问题,最好手动将路径设置在 ...

  2. 安装CMS遇到php5.3的问题

    DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recomm ...

  3. js构造函数,索引数组和属性的属性

    本文主要介绍和小结js的构造函数,关联数组的实现方式和使用,及不可变对象和它的实现方式及他们使用过程中要注意的点 <script> function p(){ var len=argume ...

  4. php输出utf-8格式

    header("Content-type:text/html;charset=utf-8"); 输出数据前插入以上代码,以utf-8格式输出,避免乱码

  5. jquery中each()函数

    今天在使用each函数时,发现写的js代码明明木有问题,为什么点击没有执行呢.js始终处于入门阶段,只好瞎鼓捣. 弄了半天,总算可以了.代码如下: <script type="text ...

  6. WordPress 主题开发 - (十二) Search模板与Page模板 待翻译

    The Search Template and The Page Template are vital to any complete WordPress Theme. And they're bot ...

  7. Linux安全运维日志排查几个 tips

    运维日志排查记录 前言 记录一些排查常见日志的命令,方法wiki,欢迎补充(Markdown 语法). 常用命令 查找关键词并统计行数 cat 2015_7_25_test_access.log | ...

  8. JS调用腾讯接口获取天气

    想做个直接通过JS获取某个城市的天气.本来想通过直接调用中国气象网的接口: http://www.weather.com.cn/weather/101070201.shtml,但是跨域问题一直无法解决 ...

  9. Bootstrap 2.3.2学习

    1.下载架包,下载编译好的文件,文件目录结构如下所示: bootstrap/ ├── css/ │ ├── bootstrap.css │ ├── bootstrap.min.css ├── js/ ...

  10. PHP中英文字符串截取函数无乱码(mb_substr)和获取中英文字符串字数函数(mb_strlen)

    mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $length,要截取的字数 $encod ...