codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想。
题目链接:codeforces 814 C. An impassioned circulation of affection
题意:给出字符串长度n (1 ≤ n ≤ 1 500),字符串s由小写字母组成,q个询问q (1 ≤ q ≤ 200 000),每个询问为:mi (1 ≤ mi ≤ n) ci ,表示可以把任意mi个字母改成ci,求每次改变后只由ci组成的最长连续字串的长度。
解法一:尺取法:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = +;
char s[N];
char c;
int n, q, m;
int main() {
scanf("%d %s", &n, s);
scanf("%d", &q);
while(q--) {
scanf("%d %c", &m, &c);
int l = , r = ;
int ans = ;
int cnt = ;
while(r < n) {
if(cnt <= m) {
if(s[r++] != c) cnt++;
}
if(cnt > m) {
if(s[l++] != c) cnt--;
}
ans = max(ans, r - l);
}
printf("%d\n", ans);
}
return ;
}
997ms
解法二:DP:打个表,暴力枚举区间,求区间内字母i没有出现的个数j,然后dp[i][j]表示换成j个字母i所求的最长长度,每次询问直接输出即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = ;
int dp[][N];
char s[N], c;
int n, q, i, j, k, num, m;
int main() {
scanf("%d %s %d", &n, s+, &q);
for(k = ; k < ; ++k) {
for(i = ; i <= n ; ++i) {
num = ;
for(j = i; j <= n; ++j) {
if(s[j]-'a' != k) num++;
dp[k][num] = max(dp[k][num], j-i+);
}
}
for(i = ; i <= n; ++i)
dp[k][i] = max(dp[k][i], dp[k][i-]);
}
while(q--) {
scanf("%d %c", &m, &c);
printf("%d\n", dp[c-'a'][m]);
}
return ;
}
156ms
解法三:还是DP。。。yy无聊打发时间。。。发呆
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = ;
int dp[][N];
char s[N], c;
int n, q, i, j, k, num, m;
int main() {
scanf("%d %s %d", &n, s+, &q);
for(i = ; i < ; ++i)
for(j = ; j <= n; ++j) dp[i][j] = j;
for(i = ; i <= n; ++i) {
for(num = , j = i; j <= n; ++j) {
if(s[j] != s[i]) num++;
if(j-i+ > dp[s[i]-'a'][num]) dp[s[i]-'a'][num] = j-i+;
}
}
for(i = ; i <= n; ++i) {
for(num = , j = n; j >= ; --j) {
if(s[j] != s[i]) num++;
if(n-j+ > dp[s[i]-'a'][num]) dp[s[i]-'a'][num] = n-j+;
}
}
for(i = ; i < ; ++i)
for(j = ; j <= n; ++j)
dp[i][j] = max(dp[i][j], dp[i][j-]);
/*for(i = 0; i < 26; ++i)
for(j = 1;j <= n; ++j)
printf("%d\t", dp[i][j]);
puts("");*/
while(q--) {
scanf("%d %c", &m, &c);
printf("%d\n", dp[c-'a'][m]);
}
return ;
}
109ms
codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】的更多相关文章
- codeforces 814 C. An impassioned circulation of affection(二分+思维)
题目链接:http://codeforces.com/contest/814/problem/C 题意:给出一串字符串然后q个询问,问替换掉将m个字符替换为字符c,能得到的最长的连续的字符c是多长 题 ...
- 【Codeforces Round 418】An impassioned circulation of affection DP
C. An impassioned circulation of affection ...
- Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection
C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 m ...
- An impassioned circulation of affection
An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 mega ...
- 【尺取或dp】codeforces C. An impassioned circulation of affection
http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...
- An impassioned circulation of affection(尺取+预处理)
题目链接:http://codeforces.com/contest/814/problem/C 题目: 题意:给你一个长度为n的字符串,m次查询,每次查询:最多进行k步修改,求字符c(要输入的字符) ...
- Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...
- Codeforces 814C - An impassioned circulation of affection
原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...
- C. An impassioned circulation of affection DP
http://codeforces.com/contest/814/problem/C 12ooyomioomioo21 o2 o 这题我是用dp解的,不过好像很慢,比赛的时候算了下不会mle,就没滚 ...
随机推荐
- 九度oj 1034 寻找大富翁 2009年浙江大学计算机及软件工程研究生机试真题
题目1034:寻找大富翁 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5323 解决:2123 题目描述: 浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁. 输入: ...
- Bootstrap使用模态框modal实现表单提交弹出框
Bootstrap 模态框(Modal)插件 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等.如果 ...
- Java ee 之 html/css样式复习
内容: 1,html/css样式 2,模拟简书注册登陆页面 *重点: 1,width:auto;height:auto; 2,background-image:url(Q5.png);left top ...
- 入门Promise的正确姿势
Promise是异步编程的一种解决方案,从语法上说,Promise是一个对象,从它可以获取异步操作的消息. Promise的基本用法 Promise构造函数接受一个函数作为参数,该函数的两个参数分别是 ...
- python 包管理工具Pipenv
Kenneth Reitz的最新工具Pipenv可以用于简化Python项目中依赖项的管理. 它汇集了Pip,Pipfile和Virtualenv的功能,是一个强大的命令行工具. 入门 首先使用pip ...
- 在浏览器中输入URL并回车后都发生了什么?
1.解析URL ________________________________________________________________________ 关于URL: URL(Universa ...
- isnull函数
isnull是判断一个字段是否为空值,返回一个特定的值 列: isnull(a,0) 如果a字段有空值返回0 没有空值就返回a的本身 isnull(a,1)=2 字段a有空值返回1,判断isnull ...
- JavaScript的六种数据类型
JavaScript数据类型有六种:number.string.boolean.null.undefined.object
- python监控linux内存并写入mongodb
(需要安装psutil 用来获取服务器资源,以及pymongo驱动)#pip install psutil #pip install pymongo #vim memory_monitory.py 文 ...
- linux 软件连接 创建/查看/删除
1.建立软链接 具体用法是:ln -s 源文件 目标文件.源:实际存放文件的位置 当 我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的 ...