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 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
随机推荐
- Linux设备驱动程序 之 open和release
open方法 open方法提供给驱动程序以初始化的能力,在大部分驱动程序汇总,open应该完成以下工作: 1. 检查特定设备的错误,如设备为准备就绪或者硬件问题: 2. 如果设备是首次打开,则对其进行 ...
- 前端知识点回顾——HTML,CSS篇
前端知识点回顾篇--是我当初刚转行为了面试而将自己学过的前端知识整理成的一份笔记,个人目的性很强,仅供参考. doctype 有什么用 doctype是一种标准通用标记语言的文档类型声明,目的是告诉标 ...
- spring boot 全局异常处理及自定义异常类
全局异常处理: 在处理controller层抛出的自定义异常时,可以实现@ControllerAdvice注解捕获,配合@ExceptionHandler来增强所有的@requestMapping方法 ...
- [go]匿名结构体
匿名结构体声明 使用匿名结构体 声明并初始化
- Kotlin函数中默认参数
Java不支持默认参数.但kotlin函数却可以 package loaderman.demo class Customer(var name:String ="name"){// ...
- easuUI之datebox日期选择框
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- SoundPool 播放短声音
SoundPool 最大只能申请1M的内存空间,只能用一些很短的声音片段,而不是用它来播放歌曲或者做游戏背景音乐. 使用 SoundPool 播放短声音实现步骤如下: // 创建SoundPool实例 ...
- JAVA 基础编程练习题15 【程序 15 排序】
15 [程序 15 排序] 题目:输入三个整数 x,y,z,请把这三个数由小到大输出. 程序分析:我们想办法把最小的数放到 x 上,先将 x 与 y 进行比较,如果 x>y 则将 x 与 y 的 ...
- C语言JS引擎
基础知识 SpiderMonkey 简介 和其他的 JavaScript 引擎一样,SpiderMonkey 不直接提供像 DOM 这样的对象,而是提供解析,执行 JavaSccript 代码,垃圾回 ...
- Go项目实战:打造高并发日志采集系统(四)
前情回顾 前文我们完成了如下目标1 项目架构整体编写2 使框架支持热更新 本节目标 在前文的框架基础上,我们1 将之前实现的日志监控功能整合到框架中.2 一个日志对应一个监控协程,当配置热更新后根据新 ...