codeforces 814 C. An impassioned circulation of affection(二分+思维)
题目链接:http://codeforces.com/contest/814/problem/C
题意:给出一串字符串然后q个询问,问替换掉将m个字符替换为字符c,能得到的最长的连续的字符c是多长
题解:预处理dp[i][j]表示第i种字幕添加j个最长的连续为多长。然后与处理一下sum[j]表示前j个里有几个不是第i种字母的。
然后for一遍二分一下。更新dp。
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;
char s[2000];
int dp[27][2000] , sum[2000];
int binsearch(int l , int r , int num , int pos) {
int mid = (l + r) >> 1;
while(l <= r) {
mid = (l + r) >> 1;
if(sum[mid] - sum[pos] <= num) l = mid + 1;
else r = mid - 1;
}
return r;
}
int main() {
int n;
scanf("%d" , &n);
scanf("%s" , (s + 1));
memset(dp , 0 , sizeof(dp));
for(int i = 0 ; i < 26 ; i++) {
int id = -1;
memset(sum , 0 , sizeof(sum));
for(int j = 1 ; j <= n ; j++) {
if(s[j] - 'a' == i) {
id = i;
break;
}
}
if(id == -1) {
for(int j = 1 ; j <= n ; j++) dp[i][j] = j;
}
else {
for(int j = 1 ; j <= n ; j++) {
sum[j] += sum[j - 1];
if(s[j] - 'a' != id) {
sum[j]++;
}
}
for(int l = 1 ; l <= n ; l++) {
for(int j = 1 ; j <= n ; j++) {
int pos = binsearch(j , n , l , j - 1);
//if(i == 14) cout << pos << endl;
dp[i][l] = max(dp[i][l] , pos - j + 1);
}
}
}
}
int q;
scanf("%d" , &q);
while(q--) {
int m;
char c[10];
scanf("%d%s" , &m , c);
int id = c[0] - 'a';
printf("%d\n" , dp[id][m]);
}
return 0;
}
codeforces 814 C. An impassioned circulation of affection(二分+思维)的更多相关文章
- 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 ...
- 【尺取或dp】codeforces C. An impassioned circulation of affection
http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...
- Codeforces 814C - An impassioned circulation of affection
原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...
- 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 ...
随机推荐
- 自定义ItemToggleView
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- spring-boot-plus1.2.0-RELEASE发布-快速打包-极速部署-在线演示
spring-boot-plus 一套集成spring boot常用开发组件的后台快速开发脚手架 Purpose 每个人都可以独立.快速.高效地开发项目! Everyone can develop p ...
- Docker系列之烹饪披萨(二)
前言 上一篇我们讲解了虚拟机和容器的区别,本节我们来讲讲Docker中关于Dockerfile.镜像.容器等基本概念.Docker是一个在容器内开发.部署.运行应用程序的平台,Docker本质上是容器 ...
- 利用python自动生成verilog模块例化模板
一.前言 初入职场,一直忙着熟悉工作,就没什么时间更新博客.今天受“利奇马”的影响,只好宅在家中,写写技术文章.芯片设计规模日益庞大,编写脚本成了芯片开发人员必要的软技能.模块端口动不动就几十上百个, ...
- 搞定java String校招面试题
今天大致的阅读了String类的源码,并刷了常见的面试题,在此做个笔记. 面试题一:判断下列程序运行结果 package String_test; public class test_1 { publ ...
- 每天用SpringBoot,还不懂RESTful API返回统一数据格式是怎么实现的?
上一篇文章RESTful API 返回统一JSON数据格式 说明了 RESTful API 统一返回数据格式问题,这是请求一切正常的情形,这篇文章将说明如何统一处理异常,以及其背后的实现原理,老套路, ...
- jquery验证大全
jQuery验证及限制 绑定键盘监听事件 $(document).on("keypress", ".txt-valid-len", function (e) { ...
- Redis回顾
之前有两篇文章着重介绍了redis集群的搭建和redis与spring的整合,一个月过去了,现在有些忘记了,今天又拿过来稳固一下,发现有很多的东西都忘记了. 资料汇总下载 首先安装ruby环境 安装过 ...
- Flink 源码解析 —— Standalone Session Cluster 启动流程深度分析之 Job Manager 启动
Job Manager 启动 https://t.zsxq.com/AurR3rN 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac ...
- .netcore持续集成测试篇之 .net core 2.1项目集成测试
系列目录 从.net到.net core以后,微软非常努力,以每年一到两个大版本的频率在演进.net core,去年相继发布了.net core 2.1和2.2,其中2.1是长期支持版,不断的快速更新 ...