题目链接: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. 北大ACM试题分类+部分解题报告链接

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  2. umask 默认权限控制和特殊权限

    权限简单介绍: 在Linux中,创建目录或者文件之后总会有默认的权限.共9个,分为三组.分别代表u.g.o(属主.属组.其他用户).r.w.x 也代表各自的权限. r:读   在文件中的权限代表次文件 ...

  3. Angualr6表单提交验证并跳转

    在Angular6中,使用NG-ZRROR作为前端开发框架,在进行表单开发时遇到了一些问题,最后解决了,在此记录. 1.表单构造: 引入forms: import { FormGroup, FormB ...

  4. 使用 PowerShell 远程管理

    要求 PowerShell 版本要求至少是2.0版本以上,目前PowerShell 2.0 支持最低的操作系统版本为Windows XP.本次操作使用的是 PowerShell 5.1 请使用管理员身 ...

  5. 动态SQL查询

    if+where: 用于查询操作,where标签可以智能判断是否添加and.or.where关键词 示例: <select id="findByParam" resultTy ...

  6. 洛谷 P1631 序列合并

    题意简述 有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N^2个和,求这N^2个和中最小的N个. 题解思路 大根堆,先存入n个和,再比较大小,改变堆中元素. 代码 #include & ...

  7. 理解-NumPy

    # 理解 NumPy 在这篇文章中,我们将介绍使用NumPy的基础知识,NumPy是一个功能强大的Python库,允许更高级的数据操作和数学计算. # 什么是 NumPy? NumPy是一个功能强大的 ...

  8. python3 how to creat alphabet

    Python: How To Generate a List Of Letters In The Alphabet  Method 1# First we need to figure out the ...

  9. js-获取屏幕的中各种宽高

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...

  10. [ZJOI2011]看电影(组合数学,高精度)

    [ZJOI2011]看电影 这题模型转化很巧妙.(神仙题) 对于这种题首先肯定知道答案就是合法方案除以总方案. 总方案显然是\(k^n\). 那么考虑怎么算合法方案. 当\(n>k\)的时候显然 ...