P2679 Sub-string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.