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 ...
随机推荐
- Mac Android 配置环境变量
进入终端,输入以下命令: cd ~ touch .bash_profile //没有该文件的话新建一个 vi .bash_profile //vim 形式打开 输入内容jdk变量配置内容: expor ...
- WebSocket的实现与应用
WebSocket的实现与应用 前言 说到websocket,就不得不提http协议的连接特点特点与交互模型. 首先,http协议的特点是无状态连接.即http的前一次连接与后一次连接是相互独立的. ...
- 3、大型项目的接口自动化实践记录----开放API练习
开始做实际项目前,先拿个网上的简单API练下手 一.API说明: 接口信息 接口名:京东获取单个商品价格 地址:http://p.3.cn/prices/mgets 入参:skuids=J_商品ID& ...
- python基础之变量与数据类型
变量在python中变量可以理解为在计算机内存中命名的一个存储空间,可以存储任意类型的数据.变量命名变量名可以使用英文.数字和_命名,且不能用数字开头使用赋值运算符等号“=”用来给变量赋值.变量赋值等 ...
- 分布式ID系列(4)——Redis集群实现的分布式ID适合做分布式ID吗
首先是项目地址: https://github.com/maqiankun/distributed-id-redis-generator 关于Redis集群生成分布式ID,这里要先了解redis使用l ...
- SVN服务器更改ip地址客户端怎么设置
SVN 服务器 IP 地址修改后,客户端对服务器的连接可以采用以下的方法重定位: 1. 如果客户端工具是TortoiseSVN,直接在工作副本上右键,选择TortoiseSVN->relocat ...
- fatal: remote origin already exists.解决方法
git remote add origin1 http://github.com/xxx/xxx.git origin名字冲突,换一个名字 遇到这种问题时表示已经有一个origin,冲突了,可能原因是 ...
- cmd命令行带参启动程序
cmd命令行带参启动程序 有一些程序不支持被直接启动,编写代码时,我们可以通过Process类来启动某个进程(某个软件),在不用代码调从而启动某个软件时,windows系统下,通常我们会用到cmd命令 ...
- React 练习项目,仿简书博客写作平台
Introduction 技术栈:react + redux + react-router + express + Nginx 练习点: redux 连接 react-router 路由跳转 scss ...
- hadoop2.7之作业提交详解(上)
根据wordcount进行分析: import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; impo ...