zoukankan      html  css  js  c++  java
  • 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 ;
    }
    
  • 相关阅读:
    751时尚公开课(三)宋杰林:时尚大跃进_豆瓣
    第九课堂-经验与技能分享交易网站
    751D·PARK北京时尚设计广场_百度百科
    2014马上有乐趣 每周六免费服装缝纫体验课_豆瓣
    设计工作室寻求合作
    【基础穿搭法】关于穿衣显高和好看的三个小技巧。(请深爱)
    给快播指一条生路:转型会员付费吧
    美华美依 | 创业谱
    服装配饰_MAVIN MARVY 高级服装定制_西服定制_衬衫定制_西装定制
    探索者系列_百度百科
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/4021873.html
Copyright © 2011-2022 走看看