P2679 Sub-string
Set $f[i][j][k][p]$ to match to the first $i$ position of a string, B string $j$ position, has matched $k$ segment, $p =0 or 1$ indicates that the bit of a string is not taken
When $p ==1$
$f [i][j][k][1]=f[i-1][j-1][k][1]+f[i-1][j-1][k-1][0]+f[i-1][j-1][k-1][1]$
$f [i][j][k][0]=f[i-1][j][k][0]+f[i-1][j][k][1]$
When $p ==0$
$f [i][j][k][1]=0$ (illegal)
$f [i][j][k][0]=f[i-1][j][k][0]+f[i-1][j][k][1]$
After blue we found that the first dimension can be scrolled array optimized out
So time $o (NMK) $ Space $o (4m^{2}) $ it'll be over.
1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineRe Register5 using namespacestd;6 Const intp=1e9+7;7 intn,m,kk,f[2][202][202][2];8 Chara[1003],b[203];9 intMain () {Tenscanf"%d%d%d",&n,&m,&KK); Onescanf"%s%s", A +1, B +1); Af[0][0][0][0]=f[1][0][0][0]=1; - for(ReintI=1, t=1; i<=n;++i,t^=1) - for(Reintj=1; j<=m;++j) the for(Reintk=1; k<=kk;++k) { - if(A[i]==b[j])//The same characters can be taken -f[t][j][k][1]= ((f[t^1][j-1][k-1][0]+f[t^1][j-1][k-1][1])%p+f[t^1][j-1][k][1])%p; - Elsef[t][j][k][1]=0; +f[t][j][k][0]= (f[t^1][j][k][0]+f[t^1][j][k][1])%p; - } +printf"%d", (f[n&1][m][kk][0]+f[n&1][m][kk][1])%p); A return 0; at}
View Code
P2679 Sub-string