Codeforces Round #354 (Div. 2) C. Vasya and String
题目链接:
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...
- Codeforces Round #184 (Div. 2) E. Playing with String(博弈)
题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...
- 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 ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
- Codeforces Round #354 (Div. 2)-C
C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...
随机推荐
- Linux安装VritualBox实现虚拟机win2003端口映射 支持远程
1. 使用VNC登录到Linux系统 2. 安装VritualBox 找到VritualBox的软件包 这里的是run格式的 可以直接在终端运行 需要几分钟时间 3.VritualBox新建虚拟机 ...
- datagridview添加复选框全选和取消
全选 private void All_selected_Click(object sender, EventArgs e) { ; i < this.DataGridViewProduct.R ...
- 常规轮询请求,客户端用Ajax调webservice的方法
服务端发布webservice,下图方框中的一定要有 客户端代码 <script type="text/javascript"> $(document).ready(f ...
- mac 下 svn ignore 操作
如何在svn中设备忽略的文件或者文件夹 1.如果你还没有对你的文件夹进行版本控制,则可以直接图形操作上进行ignore,或者在命令中运行 svn propedit svn:ignore 文件夹名 . ...
- Delphi XE5 for android 图片缩放和拖动处理
首先,需要分辨手势的类型. 有两种类型的手势: 一是标准手势(Standard Gestures): 在Windows,android上,标准手势都是用一个手指. 在Mac OS X and iOS上 ...
- 解决Genymotion下载设备失败的方法(Connection Timeout)
一直下载不下来,报错. 解决办法: 打开 C:\Users\用户名\AppData\Local\Genymobile目录 打开genymotion.log文件,在里面最下面几行,找到如下日志 [Deb ...
- jquery 匹配select下拉框与列表框
今天工作中用到 GrapyCity 的 wijmo ui 控件. 要给系统中所有类型的控件加统一样式 用法 $("input [type='text']").wijtext(); ...
- mono的https使用使用事项
private static void SetCertificatePolicy() { if( ServicePointManager.ServerCertificateValidationCall ...
- Python学习教程(learning Python)--1.4 Python数据处理基础
本节主要讨论数据操作及运算符等基础知识,熟悉C语言相关知识的读者请跳过此节. 在高级语言编程过程中,有了数据以后通常要对数据进行相应的数据处理,加.减.乘.除等基本运算,不难理解. 在Python里 ...
- C# 将汉字转化成拼音
本文来自http://www.cnblogs.com/yazdao/archive/2011/06/04/2072488.html 首先下载Visual Studio International Pa ...