[DP] HDU 5282 Senior ' s String

Source: Internet
Author: User
Tags strlen

Test instructions

Learning sister very much like the string, so the learning brother gave her two strings as a gift.

Two strings are x, Y, respectively. She is very happy, but she also wants to test Kaoxue younger brother in happy.

She defines the length of the longest common subsequence of the x and Y (the subsequence is not necessarily contiguous within the string, and a string with a length of L has a 2L subsequence, including the empty empty sequence).

Now, my sister took out all of X's subsequence lengths of L, and asked the learning brother to answer how many of them are sub-sequences of Y in these sub-sequences.

Because the answer may be very large, so the learning brother only needs to answer the final answer modulo 109+7.

Ideas:

First, we use O (N2) dynamic programming algorithm to process the DP array, Dp[i][j] represents the first I-character of the X-string and the length of the longest common subsequence of the first J characters of the Y-string, on this basis we have a dynamic planning. F[I][J] Indicates how many sub-sequences of length dp[i][j] appear in the first J characters of y in the first I character of the X-string. Transfer: If DP[I−1][J]==DP[I][J], then f[i][j]+=f[i−1][j], indicating I this character is not selected, and then consider the I this character, find the Y string before the first J characters in the X[i] match the position of the character, set to P, if dp[i−1][p−1] +1==DP[I][J], then f[i][j]+=f[i−1][p−1]. The final answer is F[n][m]. Complexity O (n2).

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <
algorithm> #include <iostream> #include <stack> using namespace std;
#define LL __int64 int dp[1234][1234],wz[1234][1234],f[1234][1234];
Char x[1555],y[1555];
int mod=1000000007;
    int main () {int t;
    cin>>t;
        while (t--) {scanf ("%s%s", x+1,y+1);
        Memset (Dp,0,sizeof (DP));
        int n,m;
        N=strlen (x+1);
        M=strlen (y+1); for (int i=1; x[i], i++) {for (int j=1; y[j]; j + +) {if (X[i]==y[j]) dp[i][j
                ]=dp[i-1][j-1]+1;
            else Dp[i][j]=max (dp[i-1][j],dp[i][j-1]);
        }} memset (Wz,0,sizeof (WZ));
            for (int i=1, i<=m; i++) {for (int j=0; j<26; j + +) Wz[i][j]=wz[i-1][j];
        wz[i][y[i]-' a ']=i;
        } memset (F,0,sizeof (f)); for (int i=0, i<=n; i++) {for (int j=0; j<=m;
                    J + +) {if (dp[i][j]==0)//empty string {f[i][j]=1;
                Continue
                } if (Dp[i][j]==dp[i-1][j]) f[i][j]+=f[i-1][j];
                if (f[i][j]>=mod) F[i][j]%=mod;
                int p=wz[j][x[i]-' a '];
                if (p==0) continue;
                if (dp[i][j]==dp[i-1][p-1]+1) f[i][j]+=f[i-1][p-1];
            if (f[i][j]>=mod) F[i][j]%=mod;
    }} printf ("%d\n", F[n][m]);
} return 0;
 }



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.