题目链接:

http://www.codeforces.com/contest/476/problem/E

E. Dreamoon and Strings

time limit per test 1 second
memory 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的更多相关文章

  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 ...

  2. 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 ...

  3. Codeforces Round #272 (Div. 2) D. Dreamoon and Sets 构造

    D. Dreamoon and Sets 题目连接: http://www.codeforces.com/contest/476/problem/D Description Dreamoon like ...

  4. Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs 水题

    A. Dreamoon and Stairs 题目连接: http://www.codeforces.com/contest/476/problem/A Description Dreamoon wa ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. echarts动态加载数据无法更新series 无法更新图表

    最近遇到一个Echarts图表无法动态更新数据的问题 最初我在option中设置series的值为一个数组,想着通过修改数组来动态更新图表,但是没变 化,后来发觉是因为图表数据会和之前的合并 看官方的 ...

  2. php设置文件类型content-type

    在PHP中可以通过header函数来发送头信息,还可以设置文件的content-type,下面整理了一些常见文件类型对于的content-type值. //date 2015-06-22//定义编码h ...

  3. STM32F4XX中断方式通过IO模拟I2C总线Master模式

    STM32的I2C硬核为了规避NXP的知识产权,使得I2C用起来经常出问题,因此ST公司推出了CPAL库,CPAL库在中断方式工作下仅支持无子地址 的器件,无法做到中断方式完成读写大部分I2C器件.同 ...

  4. 郁金香指标开源库的使用--(tulipindicators-0.8.4)

    瞎逛发现最新出了这么一个指标库,有100多种指标的函数库,文档写的比较好,重要的是作者一直在维护. 把它编成库,然后测试一下,可用于自动交易,策略交易等开发. 1.下载地址 https://githu ...

  5. 教你解决Xshell用SSH连接ubuntu总掉线该

    使用Xshell 5中的SSH连接ubuntu总是掉线,搞的自己束手无策,本集小编整理了一下原因及解决方法,现晒出来和大家分享一下,希望可以帮助大家. 1. Xshell客户端设置 将Keep Ali ...

  6. 20155210潘滢昊 2016-2017-2 《Java程序设计》第4周学习总结

    20155210 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 extends:单一继承. 抽象方法.抽象类: abstract:Java中有抽象方法的类一 ...

  7. CSS深入理解absolute

    1.图片目标来覆盖,无依赖,真不赖: 2.如何定位下拉框,最佳实践来分享: 3.对其居中或边缘,定位实现有脸面: 4.星号时有时没有,破坏队形不用愁: 5.图文对其兼容差,绝对定位来开挂: 6.文字溢 ...

  8. STM8在IAR中Printf的整形长度问题

    //ld是32位的 printf("up_intval:%ld\r\n",device_set.upload_tem); //d是16位的 printf("up_intv ...

  9. ASP.NET获取汉字拼音的首字母

    代码 #region GetChineseSpell获取汉字拼音的第一个字母 //获取汉字拼音的第一个字母 static public string GetChineseSpell(string st ...

  10. python全栈开发-面向对象-进阶2

    python_day_19 今日主要内容: 1.抽象类,接口类 2.多态 3.封装 1.抽象类,接口类 python 没有接口这个概念接口类,抽象类: 制定一个规范. 举个栗子:你的项目经理提一个需求 ...