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,就没滚 ...
随机推荐
- Codeforces 494E. Sharti
Description 有一个 \(n*n\) 的矩形,给出 \(m\) 个子矩形,这些矩形内部的点都是白色的,其余的点都是黑色,每一次你可以选择一个变长不超过 \(k\) 的正方形,满足这个正方形的 ...
- OutSystems学习笔记。
ew job and new software, new challenge as well. OutSystems这软件挺好上手的.虽然没有中文文档,但英文文档超级详细,堪称傻瓜版SOP 照着步骤写 ...
- 架构实战项目心得(八):dubbo知识的整理
1 Dubbo启动时检查.集群容错.负载均衡.线程模型的设置以及选项: 启动时检查:(1)Dubbo默认会在启动的时候检查依赖的服务是否可用,不可用时会抛出异常.阻止Spring初始化完成.以便 ...
- BAT的关于程序员的那些事
前言 你是否早有进入BAT公司的想法,但却因为对其不了解而在门外彷徨? 你是否想把技术团队打造成像BAT这些超级互联网公司,但却无从下手? 你是否已经进入了BAT,但是不知道如何晋升而苦恼? 那这篇文 ...
- 1.文本编辑器-->CKEditor+CKFinder使用与配置
一.CKEditor介绍 官网地址:http://ckeditor.com CKEditor下载地址:http://ckeditor.com/download CKFinder(免费版本)下载地址:h ...
- git提交空文件夹和删除远程文件
git提交空文件夹 在文件夹中创建 .gitkeep 文件,文件内容如下 # Ignore everything in this directory * # Except this file !.gi ...
- 四:SpringThinking
一:将对象配置到容器 1.xml文件:空参构造 <bean name="user" class="com.spring.bean.User" scope= ...
- Javaee的Dao层的抽取
有时候我们在实现不同功能的时候回看到很多的Dao层的增加.修改.删除.查找都很相似,修改我们将他们提取BaseDao 一.提取前 1. 提取前的LinkDao层: public interface L ...
- CentOS 6 安装 MySQL 8.0.+
1.先查询是否安装MySQL 大多数centos 6 自带 MySQL5.1 命令: rpm -qa|grep mysql 执行: [root@lifan ~]# rpm -qa|grep mysql ...
- SZU5
A - Couple doubi 这种题不要想复杂,直接找规律.找不出规律就打表找规律 #include <iostream> #include <string> #inclu ...