BestCoders, bestcoderhdu

Source: Internet
Author: User

BestCoders, bestcoderhdu

Senior's String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 466 Accepted Submission (s): 183


Problem DescriptionXuejiejie loves strings most. In order to win the favor of her, a young man has two strings X , Y To Xuejiejie. Xuejiejie has never seen such beautiful strings! These days, she is very happy. but Xuejiejie is missish so much, in order to cover up her happiness, she asks the young man a question. in face of Xuejiejie, the young man is flustered. so he asks you for help.

The question is that:
Define L As the length of the longest common subsequence X And Y . (The subsequence does not need to be continuous
In the string, and a string of length L Has 2L Subsequences containing the empty string). Now Xuejiejie comes up with all subsequences of length L Of string X , She wants to know the number of subsequences which is also the subsequence of string Y .
 
InputIn the first line there is an integer T , Indicates the number of test cases.

In each case:

The first line contains string X , A non-empty string consists of lowercase English letters.

The second line contains string Y , A non-empty string consists of lowercase English letters.

1 ≤ | X |, | Y | ≤ 1000 , | X | Means the length X .
OutputFor each test case, output one integer which means the number of subsequences of length L Of X Which also is the subsequence of string Y Modulo 109 + 7 .
Sample Input
2abaaab
 
Sample Output
12
 
SourceBestCoder Round #47 ($)
Recommendhujie

Note: DP d (I, j) indicates the maximum length of common substrings. f (I, j) indicates the number of substrings with the length of d (I, j) in Y.

Recursive Formula: It is very simple. Select a condition and analyze the situation.

Here we choose not to take X (I)

I. if d (I, j) = d (I-1, j) then f (I, j) + = f (I-1, j );

II. if d (I, j) = d (I-1, P-1) + 1 | X (I) = Y (p) & p <= j then f (I, j) + = f (I-1, P-1 ).

Node: Initial Conditions -- d (0, I) = d (I, 0) = 0, f (0, I) = f (0, I) = 1, f (I, j) = 0;

<pre name="code" class="cpp">#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define maxn 1005#define MOD 1000000007char s1[maxn], s2[maxn];int d[maxn][maxn];int f[maxn][maxn];int pos[maxn][27];void solve(){    int len1 = strlen(s1+1);    int len2 = strlen(s2+1);    memset(f, 0, sizeof(f));    for(int i=0; i<=len2; i++)    {        d[0][i] = d[i][0] = 0;        f[0][i] = f[i][0] = 1;    }    for(int i=1; i<=len1; i++)        for(int j=1; j<=len2; j++)            if(s1[i] == s2[j])                d[i][j] = d[i-1][j-1] + 1;            else                d[i][j] = max(d[i-1][j], d[i][j-1]);    memset(pos, 0, sizeof(pos));    for(int i=1; i<=len2; i++)    {        for(int j=0; j<26; j++)            pos[i][j] = pos[i-1][j];        pos[i][s2[i]-'a'] = i;    }    f[0][0] = 1;    for(int i=1; i<=len1; i++)        for(int j=1; j<=len2; j++)        {            if(d[i][j] == 0)            {                f[i][j] = 1;                continue;            }            if(d[i][j] == d[i-1][j])                f[i][j] = (f[i][j] + f[i-1][j]) % MOD;            int p = pos[j][s1[i]-'a'];            if(p && d[i][j] == d[i-1][p-1] + 1)                f[i][j] = (f[i][j] + f[i-1][p-1]) % MOD;        }        printf("%d\n", f[len1][len2]);}int main(){    //freopen("input.txt", "r", stdin);    //freopen("output.txt", "w", stdout);    int T;    cin>>T;    while(T--)    {        scanf("%s", s1+1);        scanf("%s", s2+1);        solve();    }    return 0;}


 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.