Dreamoon and Strings CodeForces - 477C (字符串dp)
大意: 给定字符串$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)的更多相关文章
- Maximum Questions CodeForces - 900E (字符串,dp)
大意:给定长$n$的字符串$s$, 只含'a','b','?', '?'可以替换为任意字符, 在给定长$t$的字符串, "ababab...", 求替换尽量少的'?', 使得$s$ ...
- Three Religions CodeForces - 1149B (字符串,dp)
大意: 给定字符串S, 要求维护三个串, 支持在每个串末尾添加或删除字符, 询问S是否能找到三个不相交的子序列等于三个串. 暴力DP, 若不考虑动态维护的话, 可以直接$O(len^3)$处理出最少需 ...
- Erasing Substrings CodeForces - 938F (字符串dp)
大意: 给定字符串$s$, 长度为$n$, 取$k=\lfloor log2(n)\rfloor$, 第$i$次操作删除一个长度为$2^{i-1}$的子串, 求一种方案使得, $k$次操作后$s$的字 ...
- Two strings CodeForces - 762C (字符串,双指针)
大意: 给定字符串$a$,$b$, $b$可以任选一段连续的区间删除, 要求最后$b$是$a$的子序列, 求最少删除区间长度. 删除一段连续区间, 那么剩余的一定是一段前缀和后缀. 判断是否是子序列可 ...
- 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 ...
- Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划
E. Dreamoon and Strings 题目连接: http://www.codeforces.com/contest/476/problem/E Description Dreamoon h ...
- 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 ...
- 【CODEFORCES】 C. Dreamoon and Strings
C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
随机推荐
- 利用JDK自带工具监控JVMCPU和内存指标
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- 如何选CDN:互联网大直播时代的CDN选择指南
转: 如何选CDN:互联网大直播时代的CDN选择指南 from: http://www.chnvideo.com/blog-classic-cdn.html SRS 编码器 如何选CDN:互联网 ...
- Netfilter 之 钩子函数调用
本篇主要从三层协议栈调用函数NF_HOOK说起,不断深入,分析某个钩子点中所有钩子函数的调用流程,但是本文不包含规则介绍和核心的规则匹配流程,后续文章将继续分析: NF_HOOK函数先调用了nf_ho ...
- Python list 遇到的问题
1.list“+” 运算 <list += > diff. <ndarray +=> list1 += list2是追加,而不是加法运算 list1 = [0,0,0] lis ...
- ffmpe转码添加时间水印
ffmpeg出直播流的时候 添加时间水印的话可以观察延迟 录制也方便查问题下面的命令 需要用到的字体自己百度下载一下或者到C:\Windows\Fonts复制一下 ffmpeg -stream_loo ...
- 【Webscraper】不懂编程也能爬虫
一.配置环境 在浏览器中安装web scraper插件. 所有安装包下载链接: https://pan.baidu.com/s/1CfAWf0wMO6WqicoUgdYgkg 提取码: nn2e 安装 ...
- linux内核中有哪些子系统(框架)呢?
注意: 分析用的linux内核版本为5.1.3 1. RTC子系统 2. Remote Processor子系统 3. Remote Processor Message子系统 4. SCSI子系统 5 ...
- Hibernate3主键生成策略
identity 采用数据库生成的主键,用于为long.short.int类型生成唯一标识, Oracle 不支持自增字段. <id name="id" column=&qu ...
- UiUtils
import android.app.Activity; import android.app.Dialog; import android.content.Context; import andro ...
- android Api操作SQLite数据库的示例代码
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.databa ...