题目链接: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(二分+思维)的更多相关文章

  1. codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】

    //yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...

  2. 【Codeforces Round 418】An impassioned circulation of affection DP

                                                            C. An impassioned circulation of affection   ...

  3. 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 ...

  4. An impassioned circulation of affection

    An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 mega ...

  5. 【尺取或dp】codeforces C. An impassioned circulation of affection

    http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...

  6. Codeforces 814C - An impassioned circulation of affection

    原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...

  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. jumpserver1.4.1 安装过程

    # 修改字符集 localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 export LC_ALL=zh_CN.UTF-8 echo 'LANG="zh_CN. ...

  2. hdoj 3555 BOMB(数位dp)

    //hdoj 3555 //2013-06-27-16.53 #include <stdio.h> #include <string.h> __int64 dp[21][3], ...

  3. [Abp vNext 源码分析] - 7. 权限与验证

    一.简要说明 在上篇文章里面,我们在 ApplicationService 当中看到了权限检测代码,通过注入 IAuthorizationService 就可以实现权限检测.不过跳转到源码才发现,这个 ...

  4. Logback配置文件这么写,TPS提高10倍

    通过阅读本篇文章将了解到 1.日志输出到文件并根据LEVEL级别将日志分类保存到不同文件 2.通过异步输出日志减少磁盘IO提高性能 3.异步输出日志的原理 配置文件logback-spring.xml ...

  5. linuxdeploy安装报错

    报错内容:checking installation path…fail(检查安装路径) 处理方法:安装在手机自带的存储空间中,则在路径开头加上${ENV_DIR},安装在sdcard中,加上${EX ...

  6. 7.4 元组tuple类型内置方法

    元组tuple 元组相当于不可变的列表,在定义完成后后面就不可以进行更改,因此元组只可取不可存 因为不可变,所以相对列表来说,他的功能也少了很多,所以,不难理解,他有个优点就是占用内存小. 作用:能够 ...

  7. Ubuntu下安装php7.1的gd,mysql,pdo_mysql扩展库

    执行以下命令 # apt-get install php7.1-gd # apt-get install php7.0-mysql 重新启动 php7.1-fpm(因为我是安装的 Nginx 和 ph ...

  8. socket基于TCP(粘包现象和处理)

    目录 6socket套接字 7基于TCP协议的socket简单的网络通信 AF_UNIX AF_INET(应用最广泛的一个) 报错类型 单一 链接+循环通信 远程命令 9.tcp 实例:远程执行命令 ...

  9. Python 命令行之旅 —— 深入 argparse (一)

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  10. 逆向破解之160个CrackMe —— 014

    CrackMe —— 014 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...