大意: 给定字符串$s$, $p$, 对于$0\le x\le |s|$, 求$s$删除$x$个字符后, $p$在$s$中的最大出现次数.

显然答案是先递增后递减的, 那么问题就转化求最大出现次数为$y$时, 求$S$所需要删除的最少字符数.

先暴力O(n^2)求出以每个字符开头匹配完一个$p$后的最小右端点, 然后$dp$即可.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 2e3+10;
int n, m;
char s[N], p[N];
int nxt[N], ans[N], dp[N][N]; int main() {
scanf("%s%s", s+1, p+1);
n = strlen(s+1), m = strlen(p+1);
REP(i,1,n) {
int now = 1;
nxt[i] = N-1;
REP(j,i,n) {
if (s[j]==p[now]) ++now;
if (now>m) {nxt[i]=j;break;}
}
}
memset(dp,0x3f,sizeof dp);
REP(i,0,n) dp[i][0] = 0;
REP(i,1,n) {
REP(j,1,n) dp[i][j] = min(dp[i][j], dp[i-1][j]);
REP(j,1,n) if (dp[i-1][j-1]!=INF&&nxt[i]<=n) {
dp[nxt[i]][j] = min(dp[nxt[i]][j], dp[i-1][j-1]+nxt[i]-i+1-m);
}
}
REP(i,1,n) if (dp[n][i]!=INF) ans[dp[n][i]] = i;
REP(i,1,n) ans[i] = max(ans[i], ans[i-1]);
REP(i,1,n) ans[i] = min(ans[i], (n-i)/m);
REP(i,0,n) printf("%d ", ans[i]);hr;
}

Dreamoon and Strings CodeForces - 477C (字符串dp)的更多相关文章

  1. Maximum Questions CodeForces - 900E (字符串,dp)

    大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...

  2. Three Religions CodeForces - 1149B (字符串,dp)

    大意: 给定字符串S, 要求维护三个串, 支持在每个串末尾添加或删除字符, 询问S是否能找到三个不相交的子序列等于三个串. 暴力DP, 若不考虑动态维护的话, 可以直接$O(len^3)$处理出最少需 ...

  3. Erasing Substrings CodeForces - 938F (字符串dp)

    大意: 给定字符串$s$, 长度为$n$, 取$k=\lfloor log2(n)\rfloor$, 第$i$次操作删除一个长度为$2^{i-1}$的子串, 求一种方案使得, $k$次操作后$s$的字 ...

  4. Two strings CodeForces - 762C (字符串,双指针)

    大意: 给定字符串$a$,$b$, $b$可以任选一段连续的区间删除, 要求最后$b$是$a$的子序列, 求最少删除区间长度. 删除一段连续区间, 那么剩余的一定是一段前缀和后缀. 判断是否是子序列可 ...

  5. Codeforces Round #272 (Div. 2) E. Dreamoon and Strings dp

    题目链接: http://www.codeforces.com/contest/476/problem/E E. Dreamoon and Strings time limit per test 1 ...

  6. Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划

    E. Dreamoon and Strings 题目连接: http://www.codeforces.com/contest/476/problem/E Description Dreamoon h ...

  7. E. Dreamoon and Strings(Codeforces Round #272)

    E. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

  8. 【CODEFORCES】 C. Dreamoon and Strings

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

  9. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

随机推荐

  1. springMVC课程笔记(一)springMVC架构原理分析

    一.springMVC架构原理分析 1.先搞清楚什么是springMVC: 其实springMVC是spring框架中的一个模块,springMVC和spring无需通过中间整合层整合,SpringM ...

  2. 【转】Qt编写串口通信程序全程图文讲解

    本文章原创于www.yafeilinux.com 转载请注明出处. (说明:我们的编程环境是windows xp下,在Qt Creator中进行,如果在Linux下或直接用源码编写,程序稍有不同,请自 ...

  3. centos7 下安装和配置 mongodb (重点)

    1.下载安装包 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.4.tgz 2.解压 tar -zxvf m ...

  4. linux下如何查看某个容器的详细信息?

    答: 使用docker inspect <CONTAINER ID>即可

  5. 理解MyCat分库分表

    1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

  6. UML分类

    类型                       静态图                     行为图                用例图                  交互图         ...

  7. Django之通用视图

    01-介绍 通用视图把视图开发中常用的写法和模式抽象出来,让你编写少量代码就能快速实现常见的数据视图.显示对象列表就是这样一种任务. Django 自带的通用视图能实现下述功能: 1.列出对象并显示单 ...

  8. RTSP协议-中文定义

    RTSP协议-中文定义 转自:http://blog.csdn.net/arau_sh/article/details/2982914 E-mail:bryanj@163.com 译者: Bryan. ...

  9. Linux新增开放端口

    CentOS系统 开放端口的方法: 方法一:命令行方式               1. 开放端口命令: /sbin/iptables -I INPUT -p tcp --dport 8080 -j ...

  10. R语言 我要如何开始R语言_数据分析师

    R语言 我要如何开始R语言_数据分析师 我要如何开始R语言? 很多时候,我们的老板跟我们说,这个东西你用R语言去算吧,Oh,My god!什么是R语言?我要怎么开始呢? 其实回答这个问题很简单,首先, ...