Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划

E. Dreamoon and Strings

题目连接:

http://www.codeforces.com/contest/476/problem/E

Description

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 Input

aaaaa
aa

Sample Output

2 2 1 1 0 0

Hint

题意

给你一个串S,和另外一个串P

把k从0到|S|枚举,然后问你去掉k个字符后,s串里面最多有多少个不重叠的p字符串

题解:

dp,dp[i][j]表示考虑到第i个位置,去掉了j个字符的最大值

然后我们对于每一个位置,先暴力找到最少去掉多少个,才能加一,然后暴力去转移这个玩意儿就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2005;
char s1[maxn],s2[maxn];
int dp[maxn][maxn];
int len1,len2;
int solve(int x)
{
    if(x<len2)return maxn;
    int a=x,b=len2,tmp=0;
    while(a&&b)
    {
        if(s1[a]==s2[b])a--,b--;
        else tmp++,a--;
    }
    if(b==0)return tmp;
    else return maxn;
}
int main()
{
    scanf("%s%s",s1+1,s2+1);
    len1 = strlen(s1+1);
    len2 = strlen(s2+1);
    for(int i=0;i<=len1;i++)
        for(int j=0;j<=len1;j++)
            if(j>i)dp[i][j]=-3000;
    for(int i=1;i<=len1;i++)
    {
        int x=solve(i);
        for(int k=0;k<=len1;k++)
            dp[i][k]=max(dp[i][k],dp[i-1][k]);
        for(int k=0;k<=len1;k++)if(x<=k)
            dp[i][k]=max(dp[i][k],dp[i-x-len2][k-x]+1);
    }
    for(int i=0;i<=len1;i++)
        printf("%d ",dp[len1][i]);
    printf("\n");
}

转载于:https://www.cnblogs.com/qscqesze/p/5794709.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值