http://codeforces.com/contest/814/problem/C

【题意】

给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字符串中的某些字母,最多改变m个,问操作后只包含字符ch的连续子序列最长是多少?

【思路】

方法一:

有这么一类问题,需要在给的一组数据中找到不大于某一个上限的“最优连续子序列”

于是就有了这样一种方法,找这个子序列的过程很像毛毛虫爬行方式比较流行的叫法是“尺取法”。

有关尺取的练习:

http://blog.csdn.net/acmer_sly/article/details/59524223

http://acm.hdu.edu.cn/showproblem.php?pid=5328

尺取是线性的,所以总的时间复杂度是O(qn).

方法二:
dp,对每个字母预处理,时间复杂度是O(26n^2)。

【Accepted】

 #include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=;
int n,q,m;
char s[maxn];
char ch[]; int solve(char c)
{
//双指针
int l=,r=;
int ans=;
int cnt=;
while(l<n&&r<n)
{
//右端点不断往后扫,直到不能再向右
while(r<n && (s[r]==c||cnt<m))
{
if(s[r]!=c)
{
cnt++;
}
r++;
}
//记下当前l下的解
ans=max(ans,r-l);
while(l<=r && s[l]==c)
{
l++;
}
//找到第一个使cnt-1的l,r才能继续向右更新
l++;
cnt--;
}
return ans;
}
int main()
{
while(~scanf("%d",&n))
{
scanf("%s",s);
scanf("%d",&q);
for(int i=;i<q;i++)
{
scanf("%d%s",&m,ch);
int ans=solve(ch[]);
printf("%d\n",ans);
}
}
return ;
}

尺取

 #include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
int n,q,m;
const int maxn=;
char s[maxn];
int dp[maxn][];
char ch[];
void Init()
{
memset(dp,-,sizeof(dp));
for(int c=;c<;c++)
{
for(int i=;i<n;i++)
{
int num=;
for(int k=i;k>=;k--)
{
if(s[k]==(char)(c+'a'))
{
num++;
}
//替换i-k+1-num个字母达到的子段长度是i-k+1,枚举所有的子段不断更新,找到最大值,共n^2个子段。
dp[i-k+-num][c]=max(dp[i-k+-num][c],i-k+);
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
scanf("%s",s);
Init();
scanf("%d",&q);
for(int i=;i<q;i++)
{
scanf("%d%s",&m,&ch);
if(dp[m][ch[]-'a']==-)
{
printf("%d\n",n);
}
else
{
printf("%d\n",dp[m][ch[]-'a']);
}
}
}
return ;
}

dp

【尺取或dp】codeforces C. An impassioned circulation of affection的更多相关文章

  1. Codeforces 814C - An impassioned circulation of affection

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

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

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

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

                                                            C. An impassioned circulation of affection   ...

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

  5. An impassioned circulation of affection

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

  6. An impassioned circulation of affection(尺取+预处理)

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

  7. C. An impassioned circulation of affection DP

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

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

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

  9. CF814C An impassioned circulation of affection

    思路: 对于题目中的一个查询(m, c),枚举子区间[l, r](0 <= l <= r < n),若该区间满足其中的非c字符个数x不超过m,则可以将其合法转换为一个长度为r-l+1 ...

随机推荐

  1. laravel 配置站点域名

    访问一直报404错误 laravel端: default.conf server {        listen       80;        server_name  api.xxxx.com; ...

  2. JDK使用最多的模式之一--观察者模式

    公司接到新任务,需要做一个气象监测应用.该应用将实现三个界面:当前气象状态,气象统计以及气象预报.应用从WeatherObject对象中获取所需数据:温度,湿度,气压.当然,为了可扩展性,该应用同时也 ...

  3. Web前端开发学习误区,你掉进去了没?

    从接触网站开发以来到现在,已经有五个年头了吧,今天偶然整理电脑资料看到当时为参加系里面一个比赛而做的第一个网站时,勾起了在这网站开发道路上的一串串回忆,成功与喜悦.烦恼与纠结都历历在目,感慨颇多. 先 ...

  4. ES6学习笔记(6)----函数的扩展

    参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ 函数的扩展 函数的默认值 : ES6可以为函数指定默认值 (1)指定默认值的两种方式 a.函数参 ...

  5. python pandas 中 loc & iloc 用法区别

    转自:https://blog.csdn.net/qq_21840201/article/details/80725433 ### 随机生DataFrame 类型数据import pandas as ...

  6. 做OJ项目时遇到的坑

    1.js代码写在Dom加载前,导致highcharts在ie8能够显示,而ie高版本和其他浏览器不能显示 我的理解:由于IE8和其他浏览器的js解析机制不同,ie8是在等dom全部加载完才开始执行js ...

  7. LR 两种录制:html与url

    一直在使用LR,对于Html_based script和Url-based script 两种录制方式之间,要如何选择,仍是一知半解.最近测试时遇到同样的业务功能,两种录制方式的脚本,单次执行时间差别 ...

  8. 管道命令和xargs的区别(经典解释) 自己的总结

    1. 简介 之所以能用到这个命令,关键是由于很多命令不支 持|管道来传递参数,而日常工作中有有这个必要, 所以就有了xargs命令,例如:find /sbin -perm +700 |ls -l 这个 ...

  9. iOS开发内购全套图文教程

    2015年最全的内购图文教程,首先是填各种资料,最后是代码,废话不多说,直接上图 ======================第一部分协议=============== 第一步 第二步 第三步 第四步 ...

  10. SQLite_Home

    SQLite教程 SQLite是一个库,实现了一个独立的软件,serverless zero-configuration.事务SQL数据库引擎.SQLite是世界上最广泛的部署SQL数据库引擎.SQL ...