Codeforces Round #272 (Div. 1) C. Dreamoon and Strings

C. Dreamoon and Strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.

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 test(s)
input
aaaaa
aa
output
2 2 1 1 0 0
input
axbaxxb
ab
output
0 1 1 2 1 1 0 0
Note

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

预先处理出 pre[i],cnt[i] ,pre[i]表示 和 i 匹配的最近点(也就是a[pre[i]~i]和b最长公共子序列 为 b) ,cnt[i] 是这个匹配 下需要去掉的数目

dp[i][j]表示 前 i 个字母 去掉 j 个 的最大匹配数

转移看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define INF 999999
#define maxn 2010
#define mod 1000000007
using namespace std;

int dp[maxn][maxn] ;
int pre[maxn],cnt[maxn] ;
char a[maxn],b[maxn] ;
int main()
{
    int i,n,k,j;
    int T,L,R,mid,m;
    while(scanf("%s%s",a,b) !=EOF )
    {
        memset(pre,-1,sizeof(pre)) ;
        memset(cnt,0,sizeof(cnt)) ;
        memset(dp,0,sizeof(dp)) ;
        n = strlen(a) ;
        m = strlen(b) ;
        for( i = 0; i <n ;i++)
        {
            j = 0 ;
            k = i ;
            while(j<m&&k<n)
            {
                if(b[j]==a[k])j++;
                k++;
            }
            if(j==m)pre[k]=i ,cnt[k]=k-i-m;
        }
        for(i = 1 ; i<= n ;i++)
        {
            for( j = 0 ; j <= i ;j++)
            {
                if(j) dp[i][j]=dp[i-1][j-1] ;
                if(i>j) dp[i][j] = max(dp[i][j],dp[i-1][j]) ;
                if(pre[i] != -1 && cnt[i]<=j)
                {
                    if(pre[i]>=j-cnt[i])
                    dp[i][j] = max(dp[i][j],dp[pre[i]][j-cnt[i]]+1) ;
                }
            }
        }
        cout << dp[n][0] ;
        for(i = 1 ; i <= n ;i++)
            printf(" %d",dp[n][i]) ;
        puts("") ;
    }
    return 0 ;
}

 

posted @ 2014-10-13 11:38  _log__  阅读(272)  评论(0编辑  收藏  举报