原题链接: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. linux 执行shell文件

    执行的时候总是报错 安装软件: yum install dos2unix chmod +x test.sh dos2unix test.sh 这样执行sh文件不会报一下异常,主要是因为windows中 ...

  2. python中w和wb文件写入的区别!

    一:基本区别:  w:是文本写入 wb:字节写入 windows中换行符是 \r\n w写入文件的时候,遇到 \n 自动替换成  \r\n

  3. day05—JavaScript之函数调用

    转行学开发,代码100天——2018-03-21 JavaScript中的函数调用有4种方式: 方式一:直接通过函数名调用 在 HTML 中默认的全局对象是 HTML 页面本身,所以函数是属于 HTM ...

  4. 同余dp

    先验知识: 余数的计算公式:c = a -⌊ a/b⌋ * b 其中,⌊ ⌋为向下取整运算符,向下取整运算称为Floor,用数学符号⌊ ⌋表示 题目: Consider an arbitrary se ...

  5. 【C#学习笔记】string.Format对C#字符串格式化

    文章转自:CSDN   http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格 ...

  6. 《JAVA设计模式》之适配器模式(Adapter)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述适配器(Adapter)模式的: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能 ...

  7. (三)Schema与数据类型优化

    1.Schema schema,中文叫模式,是数据库的组织和结构 2.选择优化的数据类型 更小的通常更好:尽量使用可以正确存储数据的最小数据类型 简单就好:简单数据类型的操作通常需要更少的cpu周期. ...

  8. [原]__ASSEMBLY__的用途

    在Linux Kernel中有些constant需要被C code 跟 assembler共同使用 在用constant的時候,不能單方面給0x1000UL因為assembler無法看這東西. 但是C ...

  9. python学习三十五天函数递归的用法

    python函数递归就是自己调用自己,无限循环,但是python限制了调用的次数1000次,就会终止,递归用在栏目分类,采集程序比较多,下面简单说函数递归用法和实例 1,函数递归用法 def func ...

  10. ecshop 广告调用的几种方式

    1,ECSHOP后台设置广告更换 前台调用 {insert name='ads' id=2 num=1} id值表达广告位置的id.num表示数量 2,在代码加函数 function getads($ ...