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

最新文章

  1. python安装BeautifulSoup注意事项
  2. MySQL学习笔记十六:锁机制
  3. LNMP源码编译安装(centos7+nginx1.9+mysql5.6+php7)
  4. ReactiveCocoa源码拆分解析(七)
  5. PBOC金融IC卡,卡片与终端交互的13个步骤,简介-第二组(转)
  6. 20145225《Java程序设计》 第9周学习总结
  7. 【转】 如何利用Cocos2d-x开发一个游戏
  8. java笔试题13-11-21
  9. 图解MonoForAndroid开发环境搭建
  10. java中memcached
  11. poj3090--欧拉函数
  12. 基于visual Studio2013解决C语言竞赛题之0203格式化输出
  13. C#中调用Windows API时的数据类型对应关系
  14. php IP转换整形(ip2long)
  15. android基础-界面开发注意事项
  16. 2018-2019-20175334实验一《Java开发环境的熟悉》实验报告
  17. stark组件开发之列表页面预留钩子方法。 可根据用户的不同,显示不同的列
  18. Java 8 特性 – 终极手册
  19. vue项目启动
  20. DataAnnotations 验证

热门文章

  1. php服务端接收post的json数据
  2. js数组去重的三种方式的比较
  3. html/css实现聊天布局
  4. hadoop的安装和配置
  5. fedora kde桌面系统配置
  6. IOStime处理
  7. es6 fs 输出文件 iviewDemo
  8. 通过 getResources 找不到jar包中的资源和目录的解决方法
  9. treetable
  10. python之range (范围)