题目链接:

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. ajax翻页效果模仿yii框架

    ajax翻页效果,模仿yii框架. 复制代码代码如下: <!DOCTYPE html>  <html>  <head>  <title>ajax分页_w ...

  2. php下使用phpmailer发送邮件

    由于默认虚拟空间不支持mail()函数,客户需要留言发送邮件,找到phpmailer发送不成功,调试成功后记录一下. 最新的下载地址在github,https://github.com/Synchro ...

  3. 多层级Spinner列表选项实时更新树形层级(选择城市)

    package com.example.spinnerdemo; import android.os.Bundle; import android.app.Activity; import andro ...

  4. 低功耗蓝牙(BLE)透传模块 ——RF-BM-S01(BQB认证)

    本文来源深圳信驰达科技www.szrfstar.com,技术交流群336720020. 低功耗蓝牙(BLE)透传模块 ——RF-BM-S01(BQB认证) 深圳市信驰达科技有限公司 2013年3月18 ...

  5. [.NET]程序在线更新,力求通用

    @微微一笑 :貌似是我的第一篇博客,在外流浪了很久很久,最终还是驻留在博客园混日子吧. 在线更新,想想自己做过的项目中都有这么一个功能.虽然比较简单,但是功能还是比较重要的.抽点时间写了一个在线更新程 ...

  6. Excle快速输入√与×

    如何在EXCLE中快速输入√与×呢 很简单的一个小技巧,只需要在EXCLE单元格中输入P(O) 然后将其字体设置为Wingdings 2,接着就出现符号了 不信你可以试试的哦.

  7. Python sequence (序列)

    序列简介 sequence 是一组有序元素的组合 序列可以是多个元素,也可以一个元素都没有 序列有2种:tuple(定值表).List(表) D:\python\Python_Day>pytho ...

  8. DB2行转列(多维度)

    多维度下进行行列转换,下面的行列转换时根据客户,所属银行机构进行的行列转换. -----------------建表 CREATE TABLE CUST_BANK_INFO ( CUST_ID ), ...

  9. hdu 3342 Legal or Not

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3342 Legal or Not Description ACM-DIY is a large QQ g ...

  10. 如何查看hadoop与hbase的版本匹配关系

    官网:http://hbase.apache.org/book.html 搜索:Hadoop version support matrix 下面有一个二维的支持关系表.