E. Dreamoon and Strings
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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
froms. Dreamoon wants to know  for
all x from 0 to |s| where |s| denotes
the length of string s.

Input

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.

Output

Print |s| + 1 space-separated integers in a single line representing the  for
all x from 0 to |s|.

Sample test(s)
input
aaaaa
aa
output
2 2 1 1 0 0
input
axbaxxb
ab
output
0 1 1 2 1 1 0 0
Note

For the first sample, the corresponding optimal values of s' after removal 0 through |s| = 5 characters
from s are {"aaaaa", "aaaa","aaa", "aa", "a", ""}.

For the second sample, possible corresponding optimal values of s' are {"axbaxxb", "abaxxb", "axbab", "abab", "aba", "ab","a", ""}.

dp[i][j] 为在字符串s的前i个删j个字符。k为从i開始,删除k个字符,会多出来一个字符串p。

则dp[i][j]=max(dp[i][j],dp[i-m-k][j-k]+1),m为字符串p的长度。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
const int maxn=2000+100;
using namespace std;
char s1[maxn],s2[maxn];
int dp[maxn][maxn];
int n,m;
int solve(int i)
{
int top=m,ans=0;
if(i<m)
return maxn;
while(top&&i)
{
if(s1[i]==s2[top])
top--;
else
ans++;
i--;
}
if(top)
return maxn;
else
return ans;
}
int main()
{
scanf("%s%s",s1+1,s2+1);
n=strlen(s1+1);
m=strlen(s2+1);
for(int i=0;i<=n;i++)
for(int j=i+1;j<=n;j++)
dp[i][j]=-maxn;//j>i不可能有值。赋以无穷小,防止被取到
for(int i=1;i<=n;i++)
{
int k=solve(i);
for(int j=0;j<=i;j++)
{
dp[i][j]=max(dp[i-1][j],dp[i][j]);
if(j>=k)
{
dp[i][j]=max(dp[i][j],dp[i-m-k][j-k]+1);//前面的j>i赋无穷小就是防止j-k>i-m-k时被取到。 }
}
}
for(int i=0;i<=n;i++)
{
printf("%d ",dp[n][i]);
}
return 0;
}

E. Dreamoon and Strings(Codeforces Round #272)的更多相关文章

  1. B. Dreamoon and WiFi(Codeforces Round 272)

    B. Dreamoon and WiFi time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. A. Dreamoon and Stairs(Codeforces Round #272)

    A. Dreamoon and Stairs time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. D. Dreamoon and Sets(Codeforces Round #272)

    D. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  5. Codeforces Round #272 (Div. 2) 题解

    Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...

  6. Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划

    E. Dreamoon and Strings 题目连接: http://www.codeforces.com/contest/476/problem/E Description Dreamoon h ...

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

  8. Codeforces Round #272 (Div. 1) Problem C. Dreamoon and Strings

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

  9. Codeforces Round #272 (Div. 1)C(字符串DP)

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

随机推荐

  1. PHP03 移动互联网和PHP

    学习要点 移动互联网 云计算 网络通信协议 Apache http服务器 PHP运行原理 学习目标 理解网络通信协议 掌握PHP运行原理 WAMP开发环境的搭建   移动互联网 定义 移动互联网,就是 ...

  2. OpenCV2:第七章 图像处理

    一.简介 灰度图(灰阶图),把白色到黑色之间分为256阶灰度 彩色图有RGB三个分量,假设图是800*800像素,那么就有三个800*800的矩阵分别代表RGB 二值化处理设定阈值,在阈值中的像素值变 ...

  3. win7下自动更新svn目录

    !!注意,文件编码必须是ANSI.否则中文路径会乱码 ::这里是svn安装目录 set svnExe="C:\Program Files\TortoiseSVN\bin\TortoisePr ...

  4. ubuntu install zabbix

    ubuntu install zabbix reference1 reference2 some ERRORS raise during install process, may it help. z ...

  5. Http请求封装(对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高)

    package com.ad.ssp.engine.common; import java.io.IOException; import java.util.ArrayList; import jav ...

  6. Python简介--备份

    脚本开头指定解释器路径  /usr/bin/env  python  和  /usr/bin/python比较.(Linux在PATH中找到第一个路径的Python版本来执行) PATH中第一个路径是 ...

  7. 剑指Offer(书):用两个栈实现队列

    题目:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 分析:入栈时只入栈1,出栈为栈2:若栈2不为空,直接出栈:否则,将栈1中的值依次入栈2,之后栈2出栈 Sta ...

  8. 在mac下安装matplotlib,xlrd

    brew install freetype brew install libpng sudo easy_install pip#图形显示模块 sudo pip install matplotlib 输 ...

  9. 搜狗大数据总监、Polarr 联合创始人关于深度学习的分享交流 | 架构师小组交流会

    架构师小组交流会是由国内知名公司技术专家参与的技术交流会,每期选择一个时下最热门的技术话题进行实践经验分享.第一期:来自沪江.滴滴.蘑菇街.扇贝架构师的 Docker 实践分享 第二期:来自滴滴.微博 ...

  10. [NOIP2002] 普及组

    产生数 预处理出一个数能变成多少种数,然后遍历原串的每一位,累乘方案数即可. 需要用到高精度. /*By SilverN*/ #include<iostream> #include< ...