P2679 Sub-string

Source: Internet
Author: User

Transmission Door

The very obvious DP

Set f [i] [j] [K] [1/0] indicates that the current consideration of a string I-bit, matched to the B-string J-bit, has been divided into K, is currently selected or not selected

If a [i]≠b [j] then the current bit is not selectable, then only one transfer f [i] [j] [K] [0] = f [I-1] [j] [K] [0] + f [i-1] [j] [K] [1 ]

If a [i] = B [j] then consider selecting the current bit, one more transfer

f [i] [j] [K] [1] = f [i-1] [j-1] [k-1] [0] + f [i-1] [j-1] [K] [1] + f [i-1] [j-1] [k-1] [ 1]

Note If the previous one also has a selection, then the current bit can be merged with the previous one, separate one

Note that the space is completely insufficient, so to scroll the array optimization off I

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespaceStd;typedefLong LongLl;inlineintRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&&ch<='9') {x= (x<<1) + (x<<3) + (ch^ -); Ch=GetChar ();} returnx*F;}Const intn=1007, m=207, mo=1e9+7; inlineintfkintx) {returnX>=mo? x-mo:x; }//It's quicker to take more than that .intn,m,t;CharSa[n],sb[m];intf[2][m][m][2];intMain () {n=read (); M=read (); t=read (); scanf ("%s", sa+1); scanf"%s", sb+1); f[0][0][0][0]=1;  for(intI=1; i<=n;i++)         for(intj=0; j<=m;j++)             for(intk=0; k<=t;k++) {F[i&1][j][k][0]=FK (f[(i-1) &1][j][k][0]+f[(I-1) &1][j][k][1] ); if(J&&k&&sa[i]==sb[j])//Note that at this point the j,k cannot be 0f[i&1][j][k][1]=FK (f[(i-1) &1][j-1][k][1]+ FK (f[(i-1) &1][j-1][k-1][0]+f[(I-1) &1][j-1][k-1][1]) ); Elsef[i&1][j][k][1]=0;//because it is a scrolling array, it is important to remember to assign the value to 0 if you cannot select it.} printf ("%d", FK (f[n&1][m][t][0]+f[n&1][m][t][1] )); return 0;}

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.