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. C#局部类型partial在定义实体类Model中的应用

    以前一直用继承类的方法,原来还可以这样 //例如:定义一个Person的实体类,用户ID(PersonId),姓名(Name),性别(Sex),年龄(Age),地址(Address),联系方式(Tel ...

  2. 【转】Android Support Library详细介绍

    网上对Android Support Library中各个依赖包介绍的中文资料太少了,结合官方文档和有限的参考资料做了一次总结,有描述得不对的地方还请指正. 一.主工程.依赖包.jar包.androi ...

  3. Nagios的服务器监控

    第一部分是主机外监控,比如:主机是否存活,WEB服务是否正常,MySQL服务是否正常等内容,再主机外通过访问其端口即可得知.这些监控命令再安装nagios-plugins-1.4.13.tar.gz时 ...

  4. python中的get函数

    >>> a={1:'a',2:'b'}>>> print a.get(1)a>>> print a.get(3)None

  5. codevs 3070 寻找somebody4(水题日常)

     时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold   题目描述 Description 有一天.....sb不见了,有个人要去找他..他发现sb在一个杨辉三角里.. ...

  6. 一个PHP开发APP接口的视频教程

    感觉php做接口方面的教程很少,无意中搜到了这个视频教程,希望能给一些人带来帮助http://www.imooc.com/learn/163

  7. 微信小程序开发系列七:微信小程序的页面跳转

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...

  8. 写的一个HttpClient类

    package com.ca.test.cainterface.common.util.http; import com.ca.test.cainterface.common.util.data.Da ...

  9. env - 在重建的环境中运行程序

    SYNOPSIS(总览) env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] DESCRIPTION(描述) 设置环境中的每个NAME为VAL ...

  10. dockerfile 的最佳实践

    Dockerfile 编写nginx容器 [root@mast nginx]# cat Dockerfile FROM centos MAINTAINER zhaoruidong RUN yum -y ...