//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】的更多相关文章

  1. codeforces 814 C. An impassioned circulation of affection(二分+思维)

    题目链接:http://codeforces.com/contest/814/problem/C 题意:给出一串字符串然后q个询问,问替换掉将m个字符替换为字符c,能得到的最长的连续的字符c是多长 题 ...

  2. 【Codeforces Round 418】An impassioned circulation of affection DP

                                                            C. An impassioned circulation of affection   ...

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

  4. An impassioned circulation of affection

    An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 mega ...

  5. 【尺取或dp】codeforces C. An impassioned circulation of affection

    http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...

  6. An impassioned circulation of affection(尺取+预处理)

    题目链接:http://codeforces.com/contest/814/problem/C 题目: 题意:给你一个长度为n的字符串,m次查询,每次查询:最多进行k步修改,求字符c(要输入的字符) ...

  7. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  8. Codeforces 814C - An impassioned circulation of affection

    原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...

  9. C. An impassioned circulation of affection DP

    http://codeforces.com/contest/814/problem/C 12ooyomioomioo21 o2 o 这题我是用dp解的,不过好像很慢,比赛的时候算了下不会mle,就没滚 ...

随机推荐

  1. 本地IDC机房数据库容灾解决方案

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云数据库 TencentDB 发表于云+社区专栏 作者介绍:李明,腾讯云数据库架构师华南区负责人,曾在某专业数据库服务商.51jo ...

  2. Bash编程(5) Shell方法

    shell的方法在相同的进程内执行,与调用它的脚本一致.对于方法来说,脚本中的所有变量均可见,且不需要执行export.方法中可以创建局部变量,且不影响正在调用的脚本. 1. 定义语法 (1) Kor ...

  3. 【c++】构造函数初始化列表中成员初始化的次序性

    上代码 #include <iostream> using namespace std; class A { public: A(int v): j(v + 2), i(j) {} voi ...

  4. python中文分词工具——结巴分词

    传送门: http://www.iteye.com/news/26184-jieba

  5. jqgrid 各种方法参数的使用

    现在jqGrid对象 tableObj 一.获取选中的行 selected = tableObj.jqGrid('getGridParam', 'selrow'); if (selected == n ...

  6. JAVA实现多线程处理批量发送短信、APP推送

    /** * 推送消息 APP.短信 * @param message * @throws Exception */ public void sendMsg(Message message) throw ...

  7. java向上转型的问题

    import java.util.Arrays;import java.util.HashSet;import java.util.Set;class A{ private String s1 = & ...

  8. element ui tabl 输出Html

    在使用element ui的表格的时候有遇到过表格中的数据需要换行的问题,数据是由后台传回的包含分隔符的字符串,在尝试过使用slot和直接输出html后并不能实现 解决方法:使用column的form ...

  9. redis(9)集群搭建

    一.搭建流程 以下我们将构建这样一个redis集群:三个主节点,分别备有一个从节点,主节点之间相互通信,如果主节点挂掉,从节点将被提升为主节点. redis集群至少需要3个redis实例 那么我们需要 ...

  10. vue 数组重复,循环报错

    Vue.js默认不支持往数组中加入重复的数据.可以使用track-by="$index"来实现.