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. Windows环境下安装 mysql-8.0.11-winx64 遇到的问题解决办法

    下载mysql安装包,我的是下载mysql-8.0.11-winx64,解压到你想安装的目录下,然后配置环境(window环境下,mac本还没试过), 1.首先,配置环境:右击此电脑->属性-& ...

  2. (16) Cloudflare pki公钥基础设施

    该工具组共有8个工具 1.cfssl 常用的可用指令: sign signs a certificate bundle build a certificate bundle genkey genera ...

  3. webpack 之 loader

    loader简介 loader在webpack.config.js中进行配置,配置之后,会自动检测打包过程中引入的文件(import或require),通过test成功匹配被引入的文件名后,会对文件内 ...

  4. 自定义ngCloak

    场景: 一个非常复杂的表单页面,页面有5个标签,第五个标签中又有5个标签,也就是说一共有9个标签,每个标签中都有一个表单,表单之间相互关联.所有表单项(包括复合的),有80多个数据.全部东西写在一个h ...

  5. Windows使用Nginx+ffmpeg搭建RTMP服务器

    简介Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.nginx-rmtp-module是Nginx服务器的流媒体插件.nginx通过rtmp模块提供r ...

  6. 我的java web之路(配置开发环境)

    这个学期学习java web开发,当然不能只靠上机的时间来练习,于是,便在自己的电脑上安装一系列的软件... 1.JDK的安装.首先,下载Java JDK,可以到sun公司网站下载.一定要下载适合自己 ...

  7. python常用函数 C

    1. Counter(hashable) 直接使用统计可哈希元素每个元素的数量. 2. most_common:可以统计数量最多的n个元素. from collections import Count ...

  8. Java 关于循环的练习--和为n的正数序列

    要求:输入一个正数n,输出所有和为n的连续正数序列. 分析可以从1开始连续加,若到i的和等于n则输出1到i之间的连续正数,若到i的和大于n,则改为从2开始连续加,再判断到i的和是否等于n,等于则输出2 ...

  9. python协程有多厉害?

    爬一个××网站上的东西,测算了一下协程的速度提升到底有多大,网站链接就不放了... import requests from bs4 import BeautifulSoup as sb import ...

  10. IBM MQ 创建以及常见问题集锦

    消息队列+发送队列+消息通道 接收通道名称与发送端的发送通道名称要一致,修改通道信息后要执行 start channle(chlname) 重启通道.常用的MQ命令 66.0.42.240 用户 mq ...