原题链接: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. 杂项-SAP:SAP (服务访问点(Service Accessing point))

    ylbtech-杂项-SAP:SAP (服务访问点(Service Accessing point)) 1.返回顶部 1. SAP,是Service Accessing point的缩写,意思是服务访 ...

  2. 用SPSS做时间序列

    用SPSS做时间序列 关于时间序列,有好多软件可以支持分析,大家比较熟悉的可能是EVIEWS.SPSS.还有STATA,具体用啥软件,结果都是一样的,但是SPSS作为一款学习简单,使用容易的软件还是值 ...

  3. 腾讯两大开源项目Tars、TSeer

    6月25日,在LC3(LinuxCon + ContainerCon + CloudOpen)中国2018大会上,腾讯宣布其两大开源项目——RPC开发框架Tars.轻量化名字服务方案TSeer,加入L ...

  4. 命令行下DEBIAN7时间错误的问题(转)

    Debian下的时间设置问题 Debian系统经常会遇到时间不准的情况,以下几个步骤可让您轻松摆脱烦恼: 1.设定正确的时区编辑/etc/timezone,写入Asia/Shanghai 2.使用da ...

  5. Node.js实战6:定时器,使用timer延迟执行。

    setTimeout 在nodejs中,通过setTimeout函数可以达到延迟执行的效果,这个函数也常被称为定时器. 一个简单的例子: console.log( (new Date()).getSe ...

  6. php不支持多线程怎么办

    PHP 默认并不支持多线程,要使用多线程需要安装 pthread 扩展,而要安装 pthread 扩展,必须使用 --enable-maintainer-zts 参数重新编译 PHP,这个参数是指定编 ...

  7. python:while循环语句及练习题

    while循环语句及练习题 Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务.其基本形式为: while 判断条件: 执行语句... ...

  8. 66.Subarray Sum Equals K(子数组和为K的个数)

    Level:   Medium 题目描述: Given an array of integers and an integer k, you need to find the total number ...

  9. .net core 简单集成JWT报No authenticationScheme was specified, and there was no DefaultChallengeScheme found错误

    #region JWT 认证 services .AddAuthentication(JwtBearerDefaults.AuthenticationScheme) //.AddCustomAuth( ...

  10. C#面试 笔试题 四

    1.请你简单的说明数据库建立索引的优缺点 使用索引可以加快数据的查询速度,不过由于数据插入过程中会建索引,所以会降低数据的插入.更新速度,索引还会占磁盘空间. 2.什么是WEB服务控件?使用WEB服务 ...