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 ...
随机推荐
- SQL注入详解及技巧
Tip小技巧 :在白盒测试的过程中,在sql语句的下一句加上 echo $sql. '<br>'; 可以在页面中输出完整的sql语句 效果图 :
- Rust生命周期bound用于泛型的引用
在实际编程中,可能会出现泛型引用这种情况,我们会编写如下的代码: struct Inner<'a, T> { data: &'a T, } 会产生编译错误: error[E0309 ...
- 动态开内存(malloc与calloc)
malloc与calloc 1.函数原型 #include<stdlib.h> void *malloc(unsigned int size); //申请size字节的内存 voi ...
- Apple放大绝进行反取证
取证说穿了其实就是攻防,这本是正义与邪恶的对决,亦即执法单位与嫌疑犯两者之间的事,但现实生活中要比这复杂多了. 怎么说呢?举个例子大家便理解了.取证人员费尽心思,用尽各种手法,努力地想要自手机上提取重 ...
- 简单聊聊红黑树(Red Black Tree)
前言 众所周知,红黑树是非常经典,也很非常重要的数据结构,自从1972年被发明以来,因为其稳定高效的特性,40多年的时间里,红黑树一直应用在许多系统组件和基础类库中,默默无闻的为我们提供服务,身边 ...
- Python基础编程 内置函数
内置函数 内置函数(一定记住并且精通) print()屏幕输出 int():pass str():pass bool():pass set(): pass list() 将一个可迭代对象转换成列表 t ...
- 初试kafka消息队列中间件二(采用java代码收发消息)
初试kafka消息队列中间件二(采用java代码收发消息) 上一篇 初试kafka消息队列中间件一 今天的案例主要是将采用命令行收发信息改成使用java代码实现,根据上一篇的接着写: 先启动Zooke ...
- Switch分销技术解读
Switch分销技术解读 来源:环球旅讯|2009-03-13 当Switch在海外成熟运作近40年后,该业务终于进入中国市场.但对于中国业者来说,知道Switch的人很少,了解Switch的人更少. ...
- Python.append()与Python.extend()的区别
lst=[1,2] >>>[1,2] lst.append([3,4]) >>>[1, 2, [3, 4]] lst.extend([3,4]) >>& ...
- 上个月,我赚了2W外快。。。
前段时间和室友一起给某个公司做了一个管理系统,每个人分2W多.这里和大家分享一下做完项目后一点点感受,想到啥就说点啥. 核心竞争力 两个月就挣了2W块,挣了我爸妈两个人一年的收入,每天还贼辛苦,披星戴 ...