E. Dreamoon and Strings(Codeforces Round #272)
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
froms. 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","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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #272 (Div. 2) 题解
Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...
- Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划
E. Dreamoon and Strings 题目连接: http://www.codeforces.com/contest/476/problem/E Description Dreamoon h ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- svn up 更新 校验和不匹配
BUNTU/Iproject/svn/dzradioclock-code/trunk/libs/dzlib/components/packages/DelphiXE2/dzComponentsR.dp ...
- bzoj3272 Zgg吃东西
题目描述: bz 题解: 线段树模拟费用流. 想法和种树有点类似. 每次取区间内权值和最大的一段,然后整体乘$-1$,代表再次选中时会去掉之前的影响. 线段树维护一堆东西…… 小白逛公园双倍快乐.乘$ ...
- Hibernate-04
HQL查询语法 查询:public class Dome{ Session session = HibernaeUitls.openSession(); Transaction tx = sessio ...
- KVM中的网络简介(qemu-kvm)
emu-kvm主要向客户机提供了如下4种不同模式的网络: 1)基于网桥(bridge)的虚拟网卡 2)基于NAT(Network Addresss Translation)的虚拟网络 3)QEMU内置 ...
- 如何用纯 CSS 创作一个金属光泽 3D 按钮特效
效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/full/MGeRRO 可交互视频教程 此 ...
- echart使用自定义单个柱状颜色实现
项目实践中遇到一个根据需要,当X轴等于某个值是,柱状变成特殊颜色的需求,大致有两个方案实现: 1.在前台遍历数据对象,判断设置: 2.在后台拼装数据是,按照格式要求拼装好: 手拉手,用Vue开发动态刷 ...
- Mysql 参数优化
- 基本Sql语句汇总
关于Sql语句的学习,选择的DBMS为SQL Server,Sql语句随着工作中的应用不断补充,不具备系统性,为个人笔记汇总,网上有很多优秀的资源,故不对每一处应用做过多细致的说明,后期会对部分篇幅较 ...
- jmeter-添加断言(检查点)-实例
方法/步骤 打开 jmeter的图形界面工具,然后打开之前保存的脚本(之前经验中用到的),demo-baidu.jmx 先点击运行,查看运行结果. 第一次请求返回302,然后跳转到第二次请 ...
- 72.spring boot讨论群【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 如果您碰到什么问题,您可以加群进行探讨,在群里有加入的都是Spring Boot志同道合的朋友: Spring Boot QQ交流群:193341 ...