//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想。

题目链接:codeforces 814 C. An impassioned circulation of affection

题意:给出字符串长度n (1 ≤ n ≤ 1 500),字符串s由小写字母组成,q个询问q (1 ≤ q ≤ 200 000),每个询问为:mi (1 ≤ mi ≤ n)   ci 表示可以把任意mi个字母改成ci,求每次改变后只由ci组成的最长连续字串的长度。

解法一:尺取法:

 #include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = +;
char s[N];
char c;
int n, q, m;
int main() {
scanf("%d %s", &n, s);
scanf("%d", &q);
while(q--) {
scanf("%d %c", &m, &c);
int l = , r = ;
int ans = ;
int cnt = ;
while(r < n) {
if(cnt <= m) {
if(s[r++] != c) cnt++;
}
if(cnt > m) {
if(s[l++] != c) cnt--;
}
ans = max(ans, r - l);
}
printf("%d\n", ans);
}
return ;
}

997ms

解法二:DP:打个表,暴力枚举区间,求区间内字母i没有出现的个数j,然后dp[i][j]表示换成j个字母i所求的最长长度,每次询问直接输出即可。

 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = ;
int dp[][N];
char s[N], c;
int n, q, i, j, k, num, m;
int main() {
scanf("%d %s %d", &n, s+, &q);
for(k = ; k < ; ++k) {
for(i = ; i <= n ; ++i) {
num = ;
for(j = i; j <= n; ++j) {
if(s[j]-'a' != k) num++;
dp[k][num] = max(dp[k][num], j-i+);
}
}
for(i = ; i <= n; ++i)
dp[k][i] = max(dp[k][i], dp[k][i-]);
}
while(q--) {
scanf("%d %c", &m, &c);
printf("%d\n", dp[c-'a'][m]);
}
return ;
}

156ms

解法三:还是DP。。。yy无聊打发时间。。。发呆

 #include <cstdio>
#include <algorithm>
using namespace std;
const int N = ;
int dp[][N];
char s[N], c;
int n, q, i, j, k, num, m;
int main() {
scanf("%d %s %d", &n, s+, &q);
for(i = ; i < ; ++i)
for(j = ; j <= n; ++j) dp[i][j] = j;
for(i = ; i <= n; ++i) {
for(num = , j = i; j <= n; ++j) {
if(s[j] != s[i]) num++;
if(j-i+ > dp[s[i]-'a'][num]) dp[s[i]-'a'][num] = j-i+;
}
}
for(i = ; i <= n; ++i) {
for(num = , j = n; j >= ; --j) {
if(s[j] != s[i]) num++;
if(n-j+ > dp[s[i]-'a'][num]) dp[s[i]-'a'][num] = n-j+;
}
}
for(i = ; i < ; ++i)
for(j = ; j <= n; ++j)
dp[i][j] = max(dp[i][j], dp[i][j-]);
/*for(i = 0; i < 26; ++i)
for(j = 1;j <= n; ++j)
printf("%d\t", dp[i][j]);
puts("");*/
while(q--) {
scanf("%d %c", &m, &c);
printf("%d\n", dp[c-'a'][m]);
}
return ;
}

109ms

codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】的更多相关文章

  1. codeforces 814 C. An impassioned circulation of affection(二分+思维)

    题目链接:http://codeforces.com/contest/814/problem/C 题意:给出一串字符串然后q个询问,问替换掉将m个字符替换为字符c,能得到的最长的连续的字符c是多长 题 ...

  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. An impassioned circulation of affection(尺取+预处理)

    题目链接:http://codeforces.com/contest/814/problem/C 题目: 题意:给你一个长度为n的字符串,m次查询,每次查询:最多进行k步修改,求字符c(要输入的字符) ...

  7. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)

    题目连接:http://codeforces.com/contest/676/problem/C 题意:一串字符串,最多改变k次,求最大的相同子串 题解:很明显直接尺取法 #include<cs ...

  8. Codeforces 814C - An impassioned circulation of affection

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

  9. C. An impassioned circulation of affection DP

    http://codeforces.com/contest/814/problem/C 12ooyomioomioo21 o2 o 这题我是用dp解的,不过好像很慢,比赛的时候算了下不会mle,就没滚 ...

随机推荐

  1. UIBezierPath的使用方法

    UIBezierPath的使用方式: 一,直接添加轨迹,然后stroke或者fill UIColor *blue =[UIColor blueColor]; [blue set]; UIBezierP ...

  2. python 初级/中级/高级/核心

    "一等对象": 满足条件:1.在运行时创建 2.能赋值给变量或数据结构中的元素 3.能作为参数传递给函数 4.能作为函数的返回结果 [ 整数.字符串.字典."所有函数&q ...

  3. JAVA实现多线程处理批量发送短信、APP推送

    /** * 推送消息 APP.短信 * @param message * @throws Exception */ public void sendMsg(Message message) throw ...

  4. MVVMLight - IOC Containers and MVVM

    在面向对象编程的早期,开发者要面对在应用程序或者类库中创建或检索类的实例的问题.针对这个问题有很多的解决方案.在过去几年中,依赖注入(DI)和控制反转(IoC)在开发者中很流行,并且取代了老的方案,比 ...

  5. C3P0数据库连接池的java实现

    1.配置准备 导入jar包 c3p0-0.9.2-pre1.jar mchange-commons-0.2.jar 数据库驱动包,如:mysql-connector-java-5.1.28-bin.j ...

  6. jdk各版本

    1.jdk1.7: 1.1二进制变量的表示,支持将整数类型用二进制来表示,用0b开头: 1.2  Switch语句支持string类型: 2.jdk1.8:

  7. SpringCloud实战之初级入门(二)— 服务注册与服务调用

    目录 1.环境介绍 2.服务提供 2.1 创建工程 2.2 修改配置文件 2.3 修改启动文件 2.5 亲测注意事项 3.服务调用 3.1 创建工程 3.2 修改配置文件 3.3 修改启动文件 3.4 ...

  8. CentOS 7.2 安装 MySQL 5.6.24

    说明:由于甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此CentOS社区采用mysql的MariaDB分支的方式来避开这个风险. 所以需要先加入yum的仓库,才能利用yum来安装my ...

  9. vcfc之zk+postsql+keystore(cassandra)框架分析

    vcfc框架总结: 1 一. bus和keystore是如何协调处理的,什么样的问题是处理不了的? 1. 如果在备重启的过程中,主处理了某个时间1,备机如果同步数据呢? 二 .数据可靠性和一致性分析 ...

  10. 打印thinkphp中的sql语句

    var_dump($repair->fetchSql(true)->where(array('cuername' =>$cuername))->order('applytime ...