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 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
随机推荐
- jquery ajax缓存问题解决方法小结
今天在做一个ajax数据提交功能开始利用get方式一直发现提交的数据都是一样,返回的数据也很久不刷新了,这个我知道是ajax缓存问题,后来在网上整理了一些ajax缓存问题解决方法,下面给大家分享一下. ...
- Java编程思想代码环境配置
官方代码网站已更改 https://github.com/BruceEckel/TIJ4-code 如果导入到IntelliJ中 方法1 在IntelliJ中新建一个Java项目将TIJ4-code- ...
- apidoc 接口文档系统
代码未动,文档先行.apidoc可以方便地维护接口文档.模拟响应数据.前后端分离.导出PDF文档. 特性说明 可视化编辑:支持表单界面编辑接口,不必手动编辑swagger.json 接口模拟响应:支持 ...
- SQL Labs刷题补坑记录(less54-less65)
LESS54: 只有10次尝试,dump处secret key 直接union 查就可以,括号为单引号闭合 LESS55: 尝试出来闭合的方式为)括号,后面操作与54相同 LESS56: 尝试出来括号 ...
- mysql主从复制原理及步骤
原理: 1master开启bin-log功能,日志文件用于记录数据库的读写增删2需要开启3个线程,master IO线程,slave开启 IO线程 SQL线程,3Slave 通过IO线程连接maste ...
- C++ STL——C++容器的共性和相关概念
目录 一 STL容器共性机制 二 STL容器的使用场合 三 函数对象 四 谓词 五 内建函数对象 六 函数对象适配器 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一 ...
- MybatisUtil工具类的作用
1)在静态初始化块中加载mybatis配置文件和StudentMapper.xml文件一次 2)使用ThreadLocal对象让当前线程与SqlSession对象绑定在一起 3)获取当前线程中的Sql ...
- Kafka管理与监控——彻底删除topic
一.配置 server.properties 设置 delete.topic.enable=true 如果没有设置 delete.topic.enable=true,则调用kafka 的delete命 ...
- spark-on-yarn 学习
1. hdfs存文件的时候会把文件切割成block,block分布在不同节点上,目前设置replicate=3,每个block会出现在3个节点上. 2. Spark以RDD概念为中心运行,RDD代表抽 ...
- dtcms 手机浏览
private string GetSitePath(string webPath, string requestPath, string requestDomain) { //获取当前域名包含的站点 ...