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");
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5794709.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞