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 secondmemory limit per test 256 megabytes
#### 问题描述
> Dreamoon has a string s and a pattern string p. He first removes exactly x characters from s obtaining string s' as a result. Then he calculates that is defined as the maximal number of non-overlapping substrings equal to p that can be found in s'. He wants to make this number as big as possible.
>
> More formally, let's define as maximum value of over all s' that can be obtained by removing exactly x characters from s. Dreamoon wants to know for all x from 0 to |s| where |s| denotes the length of string s.
#### 输入
> The first line of the input contains the string s (1 ≤ |s| ≤ 2 000).
>
> The second line of the input contains the string p (1 ≤ |p| ≤ 500).
>
> Both strings will only consist of lower case English letters.
#### 输出
> Print |s| + 1 space-separated integers in a single line representing the for all x from 0 to |s|.
#### 样例
> **sample input**
> aaaaa
> aa
>
> **sample output**
> 2 2 1 1 0 0
题意
给你一个文本串和一个模板串,求文本串删掉k(k=0,1,...,n)个字母后的最大不重叠子串个数。
题解
dp[i][j]表示前i个删掉j个字母后能得到的最大不重叠子串个数。
求出s1[i]结尾的往前能匹配到的位置,然后考虑这个匹配选和不选两种情况转移。
初始化的时候注意一点。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define ptf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<pair<int, int> > VPII;
const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn = 2222;
char s1[maxn],s2[maxn];
int dp[maxn][maxn];
int l1,l2;
int match(int i){
int j=l2,ret=0;
while(i>0&&j>0){
if(s1[i]==s2[j]) j--;
else ret++;
i--;
}
if(j==0) return ret;
else return -1;
}
void init(){
rep(i,0,maxn) rep(j,0,maxn) dp[i][j]=-INF;
rep(i,0,maxn){
for(int j=0;j<=i;j++){
dp[i][j]=0;
}
}
}
int main() {
scanf("%s%s",s1+1,s2+1);
l1=strlen(s1+1),l2=strlen(s2+1);
init();
for(int i=1;i<=l1;i++){
int ret=match(i);
//不选
for(int k=0;k<=i;k++){
dp[i][k]=max(dp[i][k],dp[i-1][k]);
}
if(ret<0) continue;
int j=i-ret-l2;
//选
for(int k=0;k<=i;k++){
if(k>=ret) dp[i][k]=max(dp[i][k],dp[j][k-ret]+1);
}
}
for(int i=0;i<=l1;i++) if(dp[l1][i]<0) dp[l1][i]=0;
for(int i=0;i<l1;i++) printf("%d ",dp[l1][i]);
printf("%d\n",dp[l1][l1]);
return 0;
}
//end-----------------------------------------------------------------------
Codeforces Round #272 (Div. 2) E. Dreamoon and Strings dp的更多相关文章
- Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划
E. Dreamoon and Strings 题目连接: http://www.codeforces.com/contest/476/problem/E Description Dreamoon h ...
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp
B. Dreamoon and WiFi 题目连接: http://www.codeforces.com/contest/476/problem/B Description Dreamoon is s ...
- Codeforces Round #272 (Div. 2) D. Dreamoon and Sets 构造
D. Dreamoon and Sets 题目连接: http://www.codeforces.com/contest/476/problem/D Description Dreamoon like ...
- Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs 水题
A. Dreamoon and Stairs 题目连接: http://www.codeforces.com/contest/476/problem/A Description Dreamoon wa ...
- Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)
题目链接 Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occa ...
- Codeforces Round #272 (Div. 2)-C. Dreamoon and Sums
http://codeforces.com/contest/476/problem/C C. Dreamoon and Sums time limit per test 1.5 seconds mem ...
- Codeforces Round #272 (Div. 2)-B. Dreamoon and WiFi
http://codeforces.com/contest/476/problem/B B. Dreamoon and WiFi time limit per test 1 second memory ...
- Codeforces Round #272 (Div. 2)-A. Dreamoon and Stairs
http://codeforces.com/contest/476/problem/A A. Dreamoon and Stairs time limit per test 1 second memo ...
- Codeforces Round #272 (Div. 2)C. Dreamoon and Sums 数学推公式
C. Dreamoon and Sums Dreamoon loves summing up something for no reason. One day he obtains two int ...
随机推荐
- [译文]程序员能力矩阵 Programmer Competency Matrix
注意:每个层次的知识都是渐增的,位于层次n,也蕴涵了你需了解所有低于层次n的知识. 计算机科学 Computer Science 2n (Level 0) n2 (Level 1) n (Leve ...
- 大数据:Windows下配置flink的Stream
对于开发人员来说,最希望的是需要在windows中进行测试,然后把调试好的程序放在集群中运行.下面写一个Socket,上面是监控本地的一个运行端口,来实时的提取数据.获取视频中文档资料及完整视频的伙伴 ...
- Linux学习-计算机基础
Linux 学习-计算机基础 一.描述计算机的组成及其功能. 计算机系统是由硬件(Hardware)和软件(Software )两部分组成. 硬件: 从硬件基本结构上来讲,计算机是由运算器.控制器.存 ...
- 基于 FPGA 的 PCIE 总线 Linux 驱动设计
硬件平台 Kintex ®-7 family of FPGAs Intel X86 软件平台 Linux 4.15.0-36-generic #39~16.04.1-Ubuntu Xilinx xap ...
- u-boot-1.1.6第1阶段分析之make smdk2410_config指令
uboot源码中的README文档中介绍要使用uboot必须先进行配置后编译,即先执行make xxx_config命令,然后执行make命令,下面以make smdk2410_config指令为例来 ...
- 用Python批量下载DACC的MODIS数据
本人初次尝试用Python批量下载DACC的MODIS数据,记下步骤,提醒自己,数据还在下载,成功是否未知,等待结果中...... 若有大佬发现步骤有不对之处,望指出,不胜感激. 1.下载Python ...
- 简单记录一下ruby 循环
今天整理一下ruby中的循环用法: 备注:“do~end”部分也可以写做{~} 1.break:直接跳出整个循环 i= 0 ["perl","python",& ...
- [Golang学习笔记] 08 链表
链表(Linked list)是一种常见数据结构,但并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针. 由于不必须按顺序存储,链表在插入的时候可以达到O(1),比顺序表快得多,但是查 ...
- ASP.NET 并发控制
当多个用户试图同时修改数据时,需要建立控制机制来防止一个用户的修改对同时操作的其他用户所作的修改产生不利的影响.处理这种情况的系统叫做“并发控制”. 并发控制的类型 通常,管理数据库中的并发有三种常见 ...
- 20155220 吴思其 2016-2017《java程序设计》第一周总结
对第一章和第二章的学习 通过了前两章的学习,我了解到了java的由来以及JVM/JRE/JDK三大平台 JDK JDK 是 Java 语言的软件开发工具包. JDK是整个JAVA的核心,包括了Java ...