Ultraviolet A 11081 strings (DP)

Source: Internet
Author: User

Given 3 strings of only lowercase letter you have to count the number of ways you can construct the third string by combining two subsequences from the first two strings.

After deleting 0 or more characters from a string we can get its subsequence. for example "A", "B", "C", "AB", "AC", "BC" and "ABC" all the strings are the subsequences of "ABC ". A subsequence may also be empty.

 

Now suppose there are two subsequences "ABC" and "de ". by combining them you can get the following strings "ABCDE", "abdce", "abdec", "adbce", "adbec", "adebc", "dabce ", "dabec", "daebc" and "deabc ".

 

Input

The first line of the input contains a single integer T (0 <t <271) indicating the number of test cases. each test case contains 3 strings containing only lowercase characters. the lengths of the strings are between 1 and 60.

 

Output

For each test case output a single integer denoting the number of ways you can construct the third string from the first two string by the above way. the result may be very large. you shoshould output the result % 10007.

Sample input output for sample input

2

ABC

Abbcd bccde ABCDE

 

8

18

 


Problemsetter: Abdullah-al-Mahmud

Special thanks: Md. kamruzzaman


Method To get S3 string from S1 string and S2 string:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>typedef long long LL;using namespace std;const int MOD=10007;int f[70][70][70],f1[70][70][70],f2[70][70][70];char s1[70],s2[70],s3[70];int main(){    int t;    cin>>t;    while(t--)    {        cin>>s1+1>>s2+1>>s3+1;        memset(f,0,sizeof(f));        memset(f1,0,sizeof(f1));        memset(f2,0,sizeof(f2));        int len1=strlen(s1+1);        int len2=strlen(s2+1);        int len3=strlen(s3+1);        for(int i=0;i<=len1;i++)        {            for(int j=0;j<=len2;j++)                f[i][j][0]=f1[i][j][0]=f2[i][j][0]=1;        }        for(int k=1;k<=len3;k++)        {            for(int i=0;i<=len1;i++)            {                for(int j=0;j<=len2;j++)                {                    if(i)                    {                        f1[i][j][k]=f1[i-1][j][k];                        if(s1[i]==s3[k])                            f1[i][j][k]+=f[i-1][j][k-1];                    }                    if(j)                    {                        f2[i][j][k]=f2[i][j-1][k];                        if(s2[j]==s3[k])                            f2[i][j][k]+=f[i][j-1][k-1];                    }                    f[i][j][k]=(f1[i][j][k]+f2[i][j][k])%MOD;                }            }        }        cout<<f[len1][len2][len3]<<endl;    }    return 0;}


Ultraviolet A 11081 strings (DP)

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.