NOIP2015D2T2 substring dynamic programming

Topic
This topic hits the eye
dp [i] [j] [k] dp[i][j][k]d p [ i ] [ j ] [ k ] means the number of plans!
But think about it carefully,dp dpThe d p type does not reflect the relationship between the current character and the previous string (dp dpd p formula: n You blame me for adding one dimension less?)
So we have to add another dimension0/1 0/10 / 1 indicates that the current character is connected to the a substring/ //The current character is the beginning of a new substring
0/1 0/10 / 1 apart a little more intuitive two arrays:
Weans [i] [j] [a n s [ i ] [ j ] [ k ] means toAAA- string skeweriii charactersBBB stringjjThe current number of substrings of j characters iskkNumber of answers to k
sum [i] [j] [k] sum[i][j][k]s u m [ i ] [ j ] [ k ] means currently inAAA- string skeweriii charactersBBB stringjjThe current number of substrings of j characters iskkThe number of plans at k ,
then the relational expression, how to do it?
First consider the currentans ansa n s or useA [i] A[i]A [ i ] or not,
soans [i] [j] [l] = ans [i − 1] [j] [l] + sum [i] [j] [l] ans[i][j][l ]=ans[i-1][j][l]+sum[i][j][l]a n s [ i ] [ j ] [ l ]=a n s [ i1 ] [ j ] [ l ]+s u m [ i ] [ j ] [ l ] is
obvious whenA [i] ≠ B [i] A[i]\neq B[i]A[i]=B[i] s u m sum The value of s u m is0 00
Economic intelligenceA [i] = B [i] A [i] = B [i]A[i]=B [ i ] At this time, either connect to the previous substring or create a new substring,
sosum [i] [j] [l] = sum [i − 1] [j − 1] [l] + ans [ i − 1] [j − 1] [l − 1] sum[i][j][l]=sum[i-1][j-1][l]+ans[i-1][j-1 ][l-1]sum[i][j][l]=sum[i1][j1][l]+a n s [ i1][j1][l1 ]
Haha finished? Want to be beautifuldp [1000] [200] [200] [2]??Dp
[1000][200][200][2]??d the p- [ 1 0 0 0 ] [ 2 0 0 ] [ 2 0 0 ] [ 2 ] ? ? apparently fried so we have to optimize space
noted that the first dimension looks like nothing so directly with eggs roll off the
space complexity ofO ( 4 mk) O(4mk)O ( 4 m k ) Time complexityO (nmk) O(nmk)O ( n m k ) No big problem
, code

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,m,k,now,ans[2][202][202],sum[2][202][202];
char a[1010],b[202];
int main(){
    
    
    cin>>n>>m>>k;
    scanf("%s%s",a+1,b+1);
    ans[0][0][0]=ans[1][0][0]=now=1;
    for(int i=1;i<=n;i++){
    
    
        for(int j=1;j<=m;j++){
    
    
            for(int l=1;l<=k;l++){
    
    
                if(a[i]==b[j])sum[now][j][l]=(sum[now^1][j-1][l]+ans[now^1][j-1][l-1])%mod;
                else sum[now][j][l]=0;
                ans[now][j][l]=(ans[now^1][j][l]+sum[now][j][l])%mod;
            }
        }
        now^=1;
    }
    printf("%d\n",ans[now^1][m][k]);
}

The code is not long, huh?
P. S. PSP . S . Since the operation caught between escape month+++The monthly exam is too bad+ ++ Because oftoo much food, now I have no face and half full-time(It is estimated that the head teacher and coachcan notpass both levels when applying,so I can't take the exam with the second year... There is no way I can only doNOIP NOIPN O I P The original question is to look at the knowledge points... grievances. jpg .jpg.jpg

Guess you like

Origin blog.csdn.net/dhdhdhx/article/details/102508805