原题链接:http://codeforces.com/contest/814/problem/C

题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使连续的字符c的长度最大,输出这个最大值。

思路:首先可以想到,替换两段分开的子串变成连续的字符c肯定不能得到最大值,所以替换的部分必须是连续的一个子串。那么我们可以对于a~z每个字符枚举字符区间[l,r],区间内可被替换的字符数m对应能得到r-l+1的连续字符长度,一边枚举一边更新最大值。这里我们要求得恰好能被替换成a~z字符的最大次数,当输入的m超过这个最大次数,输出字符串长度n。

AC代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
int res[][],num[][];
int maxx[];
int main()
{
char str[];
int n,q;
char ch;
scanf("%d", &n);
scanf("%s", str);
memset(res, , sizeof(res));
memset(num, , sizeof(num));
memset(maxx, , sizeof(maxx));
for(int i=;i<;i++){
for(int j=;j<=n;j++){
if(str[j-]-'a'==i)
num[i][j]=num[i][j-]+;
else
num[i][j]=num[i][j-];
num[i][j-]=j--num[i][j-];
}
num[i][n]=n-num[i][n];
}
int x;
for(int k=;k<;k++){
for(int r=;r<=n;r++){
for(int l=r;l>;l--){
x=num[k][r]-num[k][l-];
res[k][x]=max(res[k][x], r-l+);
maxx[k]=max(maxx[k], x);
}
}
} scanf("%d", &q);
int num;
for(int i=;i<q;i++){
scanf("%d %c", &num, &ch);
if(maxx[ch-'a']<=num)
printf("%d\n", n);
else
printf("%d\n", res[ch-'a'][num]);
}
return ;
}

Codeforces 814C - An impassioned circulation of affection的更多相关文章

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

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

  2. codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】

    //yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...

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

                                                            C. An impassioned circulation of affection   ...

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

  5. An impassioned circulation of affection

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

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

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

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

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

  8. C. An impassioned circulation of affection DP

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

  9. CF814C An impassioned circulation of affection

    思路: 对于题目中的一个查询(m, c),枚举子区间[l, r](0 <= l <= r < n),若该区间满足其中的非c字符个数x不超过m,则可以将其合法转换为一个长度为r-l+1 ...

随机推荐

  1. DELPHI之全局变量和局部变量

    http://www.cnblogs.com/Stwo/archive/2011/07/11/2102816.html DELPHI之全局变量和局部变量 全局变量: 如果我们在应用程序一个单元中的in ...

  2. 涛涛的小马甲 Android之Handler机制

    首先需要了解一个基本的概念ANR:Application not response 即应用程序无响应,也就是俗话说的死机. 出现Anr的原因是: 主线程需要做很多重要的事情,响应点击事件,更新UI如果 ...

  3. IDF-CTF-cookie欺骗 writeup

    题目链接: http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=40 知识点:base64解码, cookie欺骗 ...

  4. linux下shell显示git当前分支

    function git-branch-name { git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3 } functio ...

  5. 洛谷 P1731 [NOI1999]生日蛋糕(搜索剪枝)

    题目链接 https://www.luogu.org/problemnew/show/P1731 解题思路 既然看不出什么特殊的算法,显然是搜索... dfs(u,v,s,r0,h0)分别表示: u为 ...

  6. Codeforces 770C Online Courses In BSU (DFS)

    <题目链接> 题目大意:给定$n$个任务,其中有$m$个主线任务,然后$n$个任务中,每个任务都有可能有一个list,表示完成这个任务之前必须要完成的任务,然后现在让你找出,完成这$m$个 ...

  7. Linux grep常用命令

    在一个文件中同时查找多个字符串: 并集语法: grep -e 'pattern1 -e 'pattern2 file 或集语法: 1.grep -E 'pattern1|pattern2' file ...

  8. C中对整数的大端对齐与小端对齐的理解

    示例 /* 大端与小端对齐 说明: 1.对于arm, intel这种x86架构的复杂指令cpu,整数在内存中是 倒着存放的,低地址放低位,高地址放高位,称之为小端对齐 2.对于linux服务器的cpu ...

  9. html的q标签、blockquote标签

    九层之台,起于垒土 一.<q> 定义和用法 <q> 标签定义短的引用.浏览器经常在引用的内容周围添加引号. <html> <body> <p> ...

  10. 在css里如何控制表单中文本的value内的文字的位置,比方说让它向右移动2px

    方法1:比较简单的方法是将文本放到一个容器中(div,span,a等等)然后给容器设置样式,通过控制容器的位置来达到控制字体位置.(margin-left:10px; margin-top:15px; ...