【CODEFORCES】 C. Dreamoon and Strings
1 second
256 megabytes
standard input
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.
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|.
aaaaa
aa
2 2 1 1 0 0
axbaxxb
ab
0 1 1 2 1 1 0 0
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的更多相关文章
- 【CodeForces】947 D. Picking Strings
[题目]D. Picking Strings [题意]给定只含'A','B','C'的字符串,支持以下变换:1.A - BC 2.B - AC 3.C - AB 4.AAA - empty ...
- 【CODEFORCES】 A. Dreamoon and Sums
A. Dreamoon and Sums time limit per test 1.5 seconds memory limit per test 256 megabytes input stand ...
- 【CODEFORCES】 B. Dreamoon and Sets
B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【Codeforces】Round #491 (Div. 2) 总结
[Codeforces]Round #491 (Div. 2) 总结 这次尴尬了,D题fst,E没有做出来.... 不过还好,rating只掉了30,总体来说比较不稳,下次加油 A:If at fir ...
- 【Codeforces】Round #488 (Div. 2) 总结
[Codeforces]Round #488 (Div. 2) 总结 比较僵硬的一场,还是手速不够,但是作为正式成为竞赛生的第一场比赛还是比较圆满的,起码没有FST,A掉ABCD,总排82,怒涨rat ...
- 【codeforces】【比赛题解】#948 CF Round #470 (Div.2)
[A]Protect Sheep 题意: 一个\(R*C\)的牧场中有一些羊和一些狼,如果狼在羊旁边就会把羊吃掉. 可以在空地上放狗,狼不能通过有狗的地方,狼的行走是四联通的. 问是否能够保护所有的羊 ...
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
- 【Codeforces】849D. Rooter's Song
[算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...
- 【CodeForces】983 E. NN country 树上倍增+二维数点
[题目]E. NN country [题意]给定n个点的树和m条链,q次询问一条链(a,b)最少被多少条给定的链覆盖.\(n,m,q \leq 2*10^5\). [算法]树上倍增+二维数点(树状数组 ...
随机推荐
- Web前端技术体系大全搜索
一.前端技术框架 1.Vue.js 官网:https://cn.vuejs.org/ Vue CLI:https://cli.vuejs.org/ 菜鸟教程:http://www.runoob.com ...
- 教你学会Linux/Unix下的vi文本编辑器
vi编辑器是Unix/Linux系统管理员必须学会使用的编辑器.看了不少关于vi的资料,终于得到这个总结. 首先,记住vi编辑器的两个模式:1.命令模式 2.编辑模式. 在一个UNIX/Linux的s ...
- focus,focusin,blur,focusout区别
focus与focusin 1.共同点:当 <div> 元素或其任意子元素获得焦点时执行事件 2.区别:focus不支持冒泡,而focusin支持冒泡: blur与focusout 1.共 ...
- 关于JVM内存模型,GC策略以及类加载器的思考
JVM内存模型 Sun在2006年将Oracle JDK开源最终形成了Open JDK项目,两者在绝大部分的代码上都保持一致.JVM的内存模型是围绕着原子性(操作有且仅有一个结果).可见性(racin ...
- 分享一款非常好用的Fatkun图片批量下载工具
Fatkun图片批量下载 相信大家一定遇到过有着大量精美图片的网页,譬如美女照片.各种壁纸.设计素材.甚至是1024套图等等,但常常几十上百张的图要一张张手工去点击下载实在能让人抓狂!小编的工作中也常 ...
- window查看哪些端口被占用命令
管理员方式运行cmd netstat -n
- XV6陷入,中断和驱动程序
陷入,中断和驱动程序 运行进程时,cpu 一直处于一个大循环中:取指,更新 PC,执行,取指…….但有些情况下用户程序需要进入内核,而不是执行下一条用户指令.这些情况包括设备信号的发出.用户程序的非法 ...
- 动手实操(一):如何用七牛云 API 实现相片地图?
实操玩家: 在苹果手机上,我们只要打开定位服务,拍照后便能在相簿中找到地图,地图上显示着在各地拍摄的相片.网站上这种显示方式也并不少见,例如 Flickr.即将关闭的 Panoramio 等. 作为地 ...
- Iahub and Permutations(codeforces 314c)
题意:给出一组排列,某些位置不知道(-1),要求求出有多少种还原方式,使得所有a[i]!=i /* 这是一道关于排列的动态规划,这种体大都可以当作棋盘来做,如果把i这个数放到第j个位置,那么就将棋盘的 ...
- BitmapFactory.Options对图片进行缩放
package com.pingyijinren.helloworld.activity; import android.graphics.Bitmap; import android.graphic ...