My Title

noip2015 d2t2子串

考点:线性DP+前缀和+滚动数组

思路:https://www.acwing.com/solution/content/3956/

 

代码:

for(int i=1;i<=n;i++){
        f[i][0][0]=1;
        for(int j=1;j<=m;j++){
            for(int k1=1;k1<=k;k1++){
                if(a[i]==b[j]) sum[i][j][k1]=(sum[i-1][j-1][k1]+f[i-1][j-1][k1-1])%1000000007;
                else sum[i][j][k1]=0;
                f[i][j][k1]=(f[i-1][j][k1]+sum[i][j][k1])%1000000007;
            }
        }
    } 

time:O(NMK)

time:O(nmk)
time:O(nmk)
    f[0][0] = 1;
    for (int i = 1; i <= n; i ++ )
        for (int j = m; j; j -- )
            for (int k = 1; k<=kk; k++ )
            {
                if (a[i] == b[j]) sum[j][k] = (sum[j - 1][k] + f[j - 1][k - 1]) % mod;
                else sum[j][k] = 0;
                f[j][k] = (f[j][k] + sum[j][k]) % mod;
            }
滚动数组的空间优化

 

posted @ 2022-03-14 23:31  EvfX  阅读(24)  评论(0编辑  收藏  举报