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 ...
随机推荐
- tab bar controller
下面记一下怎样通过代码的方式为选项卡添加视图. 1.创建一个基于Empty Application的项目 2.创建两个新类,基类选择UIViewController,勾选With XIB for us ...
- nginx解决跨域(前后端分离)
Nginx解决跨域问题 后端接口 请求地址 返回数据(json数据) http://127.0.0.1:8080//app Hello World! 前端代码 通过nginx做静态资源服务器访问端口8 ...
- Git Bash 常用指令
1. 关于git bash常用指令 推荐博客: 史上最简单的 GitHub 教程 猴子都能懂的GIT入门 Learn Version Control with Git for Free Git Do ...
- 【HDU 6000】Wash(贪心)
Problem Description Mr.Panda is about to engage in his favourite activity doing laundry! He's brough ...
- Haybale Stacking(差分数组 + 求中位数的一些方法 + nth_element)
题意: 给定N个初始值为0的数, 然后给定K个区间修改(区间[l,r] 每个元素加一), 求修改后序列的中位数. 分析: K个离线的区间修改可以使用差分数组(http://www.cnblogs.co ...
- No unique bean of type..... Unsatisfied dependency of type
比如在XXXServiceImpl里面写了aa()方法给别的地方调用 但是自己又调用了自己 在开头写了 @Autowired Private XXX xxx; xxx.aa(); 这样重复调用自己的b ...
- MySQL之federated
由于夸服务器查询的限制,federated能够使得所有的表像是在同一台服务器上查询 (show engines-->no-->在my.ini里面添加fedrated) 经过测试,在开启fe ...
- python编程之API入门: (二)python3中使用新浪微博API
回顾API使用的流程 通过百度地图API的使用,我理解API调用的一般流程为:生成API规定格式的url->通过urllib读取url中数据->对json格式的数据进行解析.下一步,开始研 ...
- 大数据学习——redis安装
用源码工程来编译安装 / 到官网下载最新stable版 / 解压源码并进入目录 .tar.gz -C ./redis-src/ / make 如果报错提示缺少gcc,则安装gcc : yum inst ...
- bzoj 1787 Meet 紧急集合
Meet 紧急集合 这个题是在脖子oj(清北某奆佬给起的名字)八中oj(大视野在线评测)上的. 给出bzoj链接. 这个题还是求最近公共祖先的问题. 而该题不同于别的题,它是需要求三个点的最近公共祖先 ...