Codeforces 814C - An impassioned circulation of affection
原题链接: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的更多相关文章
- 【尺取或dp】codeforces C. An impassioned circulation of affection
http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...
- codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...
- 【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 ...
- codeforces 814 C. An impassioned circulation of affection(二分+思维)
题目链接:http://codeforces.com/contest/814/problem/C 题意:给出一串字符串然后q个询问,问替换掉将m个字符替换为字符c,能得到的最长的连续的字符c是多长 题 ...
- An impassioned circulation of affection(尺取+预处理)
题目链接:http://codeforces.com/contest/814/problem/C 题目: 题意:给你一个长度为n的字符串,m次查询,每次查询:最多进行k步修改,求字符c(要输入的字符) ...
- C. An impassioned circulation of affection DP
http://codeforces.com/contest/814/problem/C 12ooyomioomioo21 o2 o 这题我是用dp解的,不过好像很慢,比赛的时候算了下不会mle,就没滚 ...
- CF814C An impassioned circulation of affection
思路: 对于题目中的一个查询(m, c),枚举子区间[l, r](0 <= l <= r < n),若该区间满足其中的非c字符个数x不超过m,则可以将其合法转换为一个长度为r-l+1 ...
随机推荐
- JS-关闭当前窗口
不提示直接关闭 默认有提示,若希望不提示直接关闭则需要使用(并非兼容全部浏览器) window.opener=null; window.open('','_self'); window.close() ...
- Ubuntu 18.04 截图工具 Shutter(可以标记重点)-安装及使用
Shutter 是一个功能丰富的屏幕截图程序.您可以屏幕的某个特定区域.特定的窗口. 或者是整个屏幕,甚至一整个网站截图.可以对截图应用各种效果,标记重点,然后上 传到一个图片托管网站——所有的任务在 ...
- Spring cloud gateway自定义filter以及负载均衡
自定义全局filter package com.example.demo; import java.nio.charset.StandardCharsets; import org.apache.co ...
- 【ABAP系列】SAP 后台JOB如何DEBUG
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 后台JOB如何DEBUG ...
- poj1065Wooden Sticks(dp——最长递减数列)
Description There is a pile of n wooden sticks. The length and weight of each stick are known in adv ...
- 第五周课程总结&实验报告三
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码.结果截图.) •统计该字符串中字母s出现的次数. •统计该字符串中子串" ...
- Spring cloud学习--Zuul02
过滤器 Zuul包括两部分内容:请求的路由和过滤.而实际上请求的路由也是通过过滤器实现的,例如理由映射主要通过pre类型的过滤器完成,它将请求路径与配置的路由规则进行匹配,找到需要转发的目标地址:请求 ...
- Spring框架中Spring配置文件中<context:annotation-config/>标签说明
<context:annotation-config/>此标签的重要作用就是: 省去系统繁琐的注解标签,加上一个此标签,就可以在此项目程序添加“注解”的功能,使系统识别相应的注解功能!! ...
- android环境问题
1.android studio bundle和android studio ide区别 android studio bundle和android studio ide区别 准确的说是这三种: 最开 ...
- bfs(最短路径)
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...