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. Setting up IPS/inline for Linux in Suricata

    不多说,直接上干货! 见官网 https://suricata.readthedocs.io/en/latest/setting-up-ipsinline-for-linux.html Docs » ...

  2. 进度条--ProgressBar和BackgroundWorker

    1) 需求:就餐打卡数据处理后,插入数据库中,用进度条显示过程 2) 思路:总进度为txt文本文件的行数(数据都是按照行写入),文本文件的大小 //BackgroundWorker对象有三个主要的事件 ...

  3. Spring boot Jpa添加对象字段使用数据库默认值

    Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...

  4. Vue2.0实现路由

    Vue2.0和1.0实现路由的方法有差别,现在我用Vue 2.0实现路由跳转,话不多说,直接上代码 HTML代码 <div class="tab"> <route ...

  5. JSP报错The value for the useBean class attribute *** is invalid.

    环境:IDEA+Tomcat9+JDK1.8 在前期学习时,环境一直能够"正常"使用,实际上环境并没有完全搭建成功. 推荐: https://blog.csdn.net/lw_po ...

  6. 如何配置TomCat

    1.先查看你自己java的jdk的版本号 2.通过jdk版本号确定下载的Tomcat版本 ,因为我的是jdk 1.8的,所以要下载Tomcat 8版本 附上下载官网http://tomcat.apac ...

  7. CSData

    NSString 转换成NSData 对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncodin ...

  8. How `new’ operator works ?

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=15 February 15, 2013 How `new’ operator ...

  9. CCS3超长文字显示省略号的方法

    需求:当文本长度溢出包含元素时以省略号结尾 CSS3实现方法: #MyDIV{overflow:hidden;text-overflow:ellipsis;} 示例:<!DOCTYPE html ...

  10. PHP memcache扩展安装 for Windows

    一.下载并安装memcached服务器端软件    1.下载memcached软件 32位下载地址: memcached-win32-1.4.4-14.zip(直接下载),memcached-win3 ...