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. 洛谷——P4932 浏览器

    P4932 浏览器 __stdcall给了你n个点,第i个点有权值x[i],对于两个点u和v,如果x[u] xor x[v]的结果在二进制表示下有奇数个1,那么在u和v之间连接一个Edge,现在__s ...

  2. 洛谷P2802 回家

    贱呼呼的搜索题 这个最贱的还是在于路途的标记,大部分的题目路途的标记是直接标记即可也就是说我走过了这个点,那么这个点标记上以后不再走,这个题不是,我走过了,但是我可能回了血我又继续走 所以说我们标记的 ...

  3. 如何使用MySQL一个表中的字段更新另一个表中字段

    [本文出自:https://www.jb51.net/article/150323.htm] 这篇文章主要介绍了如何使用MySQL一个表中的字段更新另一个表中字段,需要的朋友可以参考下 1,修改1列 ...

  4. python TCP协议详解 三次握手四次挥手和11种状态

    11种状态解析 LISTEN  --------------------  等待从任何远端TCP 和端口的连接请求. SYN_SENT  ---------------  发送完一个连接请求后等待一个 ...

  5. 网易技术分享:Nginx缓存引发的跨域惨案

    推荐:更多技术团队分享文章 关注:MAYOU18技术专栏 1. 前言 贵金属wap版直播间上线后,偶尔有用户反馈,在进入wap直播间的时候,出现空白页面,但是重新刷新又可以正常显示了.我们曾一度认为是 ...

  6. alt、title和label

    alt是html标签的属性,而title既是html标签,又是html属性. title标签这个不用多说,网页的标题就是写在<title></title>这对标签之内的. ti ...

  7. C语言学习9

    婚礼的谎言 三对情侣参加婚礼,三个新郎为A.B.C,三个新娘为X.Y.Z.有人想知道究竟水域谁结婚2,于是就问新人中的三位,得到结果如下:A说他将和X结婚:X说她的未婚夫是C:C说他将和Z结婚.这人事 ...

  8. Saving James Bond - Hard Version

    07-图5 Saving James Bond - Hard Version(30 分) This time let us consider the situation in the movie &q ...

  9. 大数据学习——hadoop的RPC框架

    项目结构 服务端代码 test-hadoop-rpc pom.xml <?xml version="1.0" encoding="UTF-8"?> ...

  10. 常见的 Android 新手误区

    在过去十年的移动开发平台中,作为资深的移动开发人员,我们认为Android平台是一个新手最广为人知的平台.它不仅是一个廉价的工具,而且有着良好的 开发社区,以及从所周知的编程语言(Java),使得开发 ...