Vijos [1982] NOIP2015Day2T2 substring dynamic planning, noip2015substring

Source: Internet
Author: User

Vijos [1982] NOIP2015Day2T2 substring dynamic planning, noip2015substring
Substring (substring. cpp/c/pas) question link

[Problem description]
There are two strings A and B that only contain lowercase English letters. Now we need to extract k non-empty substrings that do not overlap with each other from string A, and then connect these k substrings in the order they appear in string A To Get
New String. How many solutions can this new string be equal to string B? Note: The position of the substring is different.
[Input format]
The input file name is substring. in.
The first line is the three positive integers n, m, and k, respectively representing the length of string A, the length of string B, and the k mentioned in the Problem description, each integer is separated by a space.
The second line contains A string of n, indicating string.
The third line contains a string of m, indicating string B.
[Output format]
The output file name is substring. out.
The output contains an integer representing the number of solutions. Because the answer may be large, the output result of Modulo 1,000,000,007 is required.
[Input and Output Example 1]
Substring. in
6 3 1
Aabaab
Aab
Substring. out
2
[Input and Output Example 2]
Substring. in
6 3 2
Aabaab
Aab
Substring. out
7
[Input and Output Example 3]
Substring. in
6 3 3
Aabaab
Aab
Substring. out
7

[Question]
NOIP2015Day2T2
A good DP question
We use dp [I] [j] [k] to indicate that I are matched in string B, and the position matched in string A is j, total number of solutions using k substrings, then dp [I] [j] [k] = Σ dp [I-1] [j '] [k-1] + dp [I-1] [J-1] [k]
Then, the prefix and optimization can be used for Σ, so that the time can be stuck in, but the space still needs to be blown up, so we can use a rolling array to optimize the space. For details, see the code.

#include <cstring>#include <algorithm>#include <cstdio>#include <cstdlib>#include <cmath>using namespace std;const int N=1000+5,M=200+5;const int mod=1e9+7;char s1[N],s2[M];int n,m,K;int dp[2][N][M],sum[2][N][M];//dp[i][j][k]=Σdp[i-1][j'][k-1](1<=j'<=j-1) +dp[i-1][j-1][k] int main(){    scanf("%d%d%d%s%s",&n,&m,&K,s1+1,s2+1);    memset(dp,0,sizeof dp);    memset(sum,0,sizeof sum);    int I=0,J=1;    for (int i=1;i<=n;i++){        if (s2[1]==s1[i])            dp[0][i][1]=1;        sum[0][i][1]=sum[0][i-1][1]+dp[0][i][1];    }    for (int i=2;i<=m;i++,I^=1,J^=1){        memset(dp[J],0,sizeof dp[J]);        memset(sum[J],0,sizeof sum[J]);        for (int j=1;j<=n;j++){            if (s2[i]!=s1[j])                continue;            for (int k=1;k<=K;k++)                if (j>=2)                    dp[J][j][k]=(sum[I][j-1][k-1]+dp[I][j-1][k])%mod;                else                    dp[J][j][k]=dp[I][j-1][k];        }        for (int k=1;k<=K;k++)            for (int j=1;j<=n;j++)                sum[J][j][k]=(sum[J][j-1][k]+dp[J][j][k])%mod;    }    printf("%d",sum[I][n][K]);    return 0;}

 

 

 



Related Article

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.