C. 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
from s. 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",

题解:这一题是DP。用D[i][j]表示从1至i。已经取走j个字符时的最大匹配串数。那么,第i位就有2种决策。取还是不取。而不取又分两种情况,一种是与前面的字符串没有能匹配的,一种是和前面的字符串有能匹配的。所以我们能够将状态转移方程表演示样例如以下:

D[i][j]=max(D[i-1][k-1],D[i-1][k])    //取还是不取

D[i][j]=max(D[i][j],D[k][j-(i-k-l2)])   //假设与前面有能匹配的情况

分析清楚了以后。能够发现这个问题跟最长不下降子序列事实上非常像。接下来就是编程实现了。要注意边界。D[i][j]的j不能大于i。并且不能为负数。

#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; char s1[2005],s2[2005];
int l1,l2,j,d[2005][2005],f; int mac(int i)
{
int x=i,y=l2;
while (x && y)
{
if (!y) break;
if (s1[x-1]==s2[y-1]) {x--; y--;}
else x--;
}
if (!y)
{
j=x;
return 1;
}
else
{
j=0;
return 0;
}
} int main()
{
scanf("%s",s1);
scanf("%s",s2);
l1=strlen(s1);
l2=strlen(s2);
for (int i=1;i<=l1;i++)
{
f=mac(i);
for (int k=0;k<=i;k++)
{
d[i][k]=max(d[i-1][k],d[i-1][k-1]);
if (f && j>=k-i+j+l2 && k-i+j+l2>=0) d[i][k]=max(d[i][k],d[j][k-(i-j-l2)]+1);
}
}
for (int i=0;i<=l1;i++)
printf("%d ",d[l1][i]);
return 0;
}

【CODEFORCES】 C. Dreamoon and Strings的更多相关文章

  1. 【CodeForces】947 D. Picking Strings

    [题目]D. Picking Strings [题意]给定只含'A','B','C'的字符串,支持以下变换:1.A - BC   2.B - AC   3.C - AB   4.AAA - empty ...

  2. 【CODEFORCES】 A. Dreamoon and Sums

    A. Dreamoon and Sums time limit per test 1.5 seconds memory limit per test 256 megabytes input stand ...

  3. 【CODEFORCES】 B. Dreamoon and Sets

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

  4. 【Codeforces】Round #491 (Div. 2) 总结

    [Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...

  5. 【Codeforces】Round #488 (Div. 2) 总结

    [Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...

  6. 【codeforces】【比赛题解】#948 CF Round #470 (Div.2)

    [A]Protect Sheep 题意: 一个\(R*C\)的牧场中有一些羊和一些狼,如果狼在羊旁边就会把羊吃掉. 可以在空地上放狗,狼不能通过有狗的地方,狼的行走是四联通的. 问是否能够保护所有的羊 ...

  7. 【CodeForces】601 D. Acyclic Organic Compounds

    [题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...

  8. 【Codeforces】849D. Rooter's Song

    [算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...

  9. 【CodeForces】983 E. NN country 树上倍增+二维数点

    [题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...

随机推荐

  1. android 图片

    1,setCompoundDrawables(Drawable left, Drawable top,Drawable right, Drawable bottom) 设置图片出现在textView, ...

  2. 正则表达式入门教程&&经典Javascript正则表达式----share

    前言 例子: ^.+@.+\\..+$ 这样的代码曾经多次把我自己给吓退过.可能很多人也是被这样的代码给吓跑的吧.继续阅读本文将让你也可以自由应用这样的代码. 正文 教程:正则表达式30分钟入门教程 ...

  3. 【简●解】[ZJOI2005]午餐

    [简●解][ZJOI2005]午餐 [关键词] \(DP\) 排序/贪心 [分析] 首先,一个很明显的贪心思路,就是吃的慢的人先打饭.所以把数据按吃饭时间从大到小排一遍序. 根据\(dp\)的尿性,比 ...

  4. 微信小程序wx.request请求服务器json数据并渲染到页面

    [原文出自]: https://blog.csdn.net/weixin_39927850/article/details/79766259 微信小程序的数据总不能写死吧,肯定是要结合数据库来做数据更 ...

  5. linux下使用docker方式部署ftp服务器

    linux环境下部署vsftpd比较繁琐,可以直接使用docker官方仓库中的pure-ftpd镜像来部署ftp服务器. 下载镜像 docker pull stilliard/pure-ftpd:ha ...

  6. linux 定时任务(注意事项)

    1.在要执行的脚本中,执行其他脚本时,需要加入其他脚本需要的环境变量. 2.路径要写全,绝对路径.命令要写全,使用绝对路径的方式.

  7. 【HDU 6005】Pandaland(Dijkstra)

    Problem Description Mr. Panda lives in Pandaland. There are many cities in Pandaland. Each city can ...

  8. 如何将数据放入下拉框List值

    最近在做下拉框,里面放入值大概有这几种 //仓库业务类型 第一种 model.addAttribute("warehouseBizTypeList", basePropertySe ...

  9. .NET Core 文件的上传与下载

    1.前言 文件导入导出是简单且常用的功能,以下示例实现了文件上传与下载的几种方法,如有不妥的地方,欢迎指正.小白一枚,在往目标前进. 2.1.文件上传    --    纯文件 <form ac ...

  10. 【java基础 3】树形结构数据呈现的递归算法实现

    一.基本概况 在我的项目中,常常会用到树形结构的数据,最为明显的就是左边菜单栏,类似于window folder一样的东西. 而我之前一直是借助前端封装好的ZTree等工具实现展示,而后台则通常使用递 ...