【尺取或dp】codeforces C. An impassioned circulation of affection
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的更多相关文章
- Codeforces 814C - An impassioned circulation of affection
原题链接:http://codeforces.com/contest/814/problem/C 题意:有长度为n的一个字符串,q个询问,每个询问由数字m和字符c组成,问最多在字符串中替换m个字符,使 ...
- codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
//yy:因为这题多组数据,DP预处理存储状态比每次尺取快多了,但是我更喜欢这个尺取的思想. 题目链接:codeforces 814 C. An impassioned circulation of ...
- 【Codeforces Round 418】An impassioned circulation of affection DP
C. An impassioned circulation of affection ...
- 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 ...
- An impassioned circulation of affection
An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 mega ...
- An impassioned circulation of affection(尺取+预处理)
题目链接:http://codeforces.com/contest/814/problem/C 题目: 题意:给你一个长度为n的字符串,m次查询,每次查询:最多进行k步修改,求字符c(要输入的字符) ...
- C. An impassioned circulation of affection DP
http://codeforces.com/contest/814/problem/C 12ooyomioomioo21 o2 o 这题我是用dp解的,不过好像很慢,比赛的时候算了下不会mle,就没滚 ...
- codeforces 814 C. An impassioned circulation of affection(二分+思维)
题目链接:http://codeforces.com/contest/814/problem/C 题意:给出一串字符串然后q个询问,问替换掉将m个字符替换为字符c,能得到的最长的连续的字符c是多长 题 ...
- CF814C An impassioned circulation of affection
思路: 对于题目中的一个查询(m, c),枚举子区间[l, r](0 <= l <= r < n),若该区间满足其中的非c字符个数x不超过m,则可以将其合法转换为一个长度为r-l+1 ...
随机推荐
- webform简单空间以及数据库访问
1.简单控件 Label - 文字,编译后显示的是<span> 一说到边框:1.颜色 2.类型,比如solid实线3.width宽度Literal -里面可以承载很多东西,比如文字,比如a ...
- php 缓存工具类 实现网页缓存
php 缓存工具类 实现网页缓存 php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存 一.文件缓存 二.数据查询结果缓存,使用内存来实现高速缓存 本 ...
- mac下fiddler安装配置启动及iphone配置连接
Getting started 下载安装Mono 如果没有下载则下载:https://www.mono-project.com/download/stable/#download-mac 从Mozil ...
- SEO 第六章
SEO第六章 本次课程目标: 1. 掌握网站的内链优化 2. 网站的URL优化 一. 网站地图 1. 什么是网站地图? 网站地图也叫站点地图,英文名叫sitemap,指的网站所有内 ...
- DROP FUNCTION - 删除一个函数
SYNOPSIS DROP FUNCTION name ( [ type [, ...] ] ) [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP FUNCTION ...
- 用Vue的方式实现复选框
var borrVm = new Vue({ el: "#WingApp", data: { returnBookList:[], checked:"", ch ...
- android 解密工具
androguard. 这个是python写的 安装: pip install androguard
- 沈南鹏@《遇见大咖》: A轮没投,投了8个月以后就证明了张一鸣是对了,在美国都没有张一鸣这种模式
沈南鹏@<遇见大咖>: A轮没投,投了8个月以后就证明了张一鸣是对了,在美国都没有张一鸣这种模式
- RN传参的问题
RN父组件通过props属性给子组件传参,假设参数 target={target} 子组件在render函数里 let { target } = this.props; 如果子组件有个 FlatLis ...
- 去除ie下select框箭头
select::-ms-expand { display: none;}兼容ie10及以上