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 ...
随机推荐
- Python List extend()方法
Python List extend()方法 Python 列表 描述 extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表). 语法 extend()方法语法 ...
- linux破解root登录密码,并重置
重启系统后按'e'键,进入编辑模式,在'UTF -8'后空格输入'rd.break'后,按快捷键'Ctrl+X'进入新界面进行编辑,代码如下: switch_root:/# mount -o remo ...
- tomcat7使用dbcp连接池遇到的坑
项目部署在tomcat后每隔一段时间便会报错 Cause: java.sql.SQLException: Could not retrieve transation read-only status ...
- git 项目相关
工具篇:Sourcetree 和 Git Bash Sourcetree Git一款非常好用的可视化工具,方便管理项目.下载地址 https://www.sourcetreeapp.com/ Git ...
- XV6锁
锁 xv6 运行在多处理器上,即计算机上有多个单独执行代码的 CPU.这些 CPU 操作同一片地址空间并分享其中的数据结构:xv6 必须建立一种合作机制防止它们互相干扰.即使是在单个处理器上,xv6 ...
- OO第二次作业
第一次作业: 由于第一次作业的调度较为简单,采用FIFO策略,以及不支持捎带功能,因此我的第一次电梯作业并没有设置单独的调度器,而会直接将任务交给电梯,电梯进行调度策略也仅为先运动到people的In ...
- FZU-2148-Moon Game,,几何计算~~
Problem 2148 Moon Game Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description Fat brothe ...
- bzoj 3207 花神的嘲讽计划Ⅰ 主席树+hash
花神的嘲讽计划Ⅰ Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3112 Solved: 1086[Submit][Status][Discuss] ...
- 【HDOJ6312】Game(博弈)
题意: 有一个1到n的序列,两个人轮流取数,取走一个数同时会取走它所有的因子,不能取者为输,两个人都按最优策略取数,问先手是否必胜 思路: #include<cstdio> #includ ...
- linux awk常用命令【转载】
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...