lightoj 1013 Love Calculator (LCS+dp)

来源:互联网 发布:淘宝开店怎么卖 编辑:程序博客网 时间:2024/04/29 15:12

Yes, you are developing a 'Love calculator'. The software would be quite complex such that nobody could crack the exact behavior of the software.

So, given two names your software will generate the percentage of their 'love' according to their names. The software requires the following things:

1.                  The length of the shortest string that contains the names as subsequence.

2.                   Total number of unique shortest strings which contain the names as subsequence.

Now your task is to find these parts.


Input

Input starts with an integer T (≤ 125), denoting the number of test cases.

Each of the test cases consists of two lines each containing a name. The names will contain no more than30 capital letters.

Output

For each of the test cases, you need to print one line of output. The output for each test case starts with the test case number, followed by the shortest length of the string and the number of unique strings that satisfies the given conditions.

You can assume that the number of unique strings will always be less than 263. Look at the sample output for the exact format.

Sample Input

3

USA

USSR

LAILI

MAJNU

SHAHJAHAN

MOMTAJ

Sample Output

Case 1: 5 3

Case 2: 9 40

Case 3: 13 15


题意:给出两个字符串,求一个字符串,使得另两个字符串为该串的子序列(也就是说不必每个字母都相邻),求该串的最小长度,并求出在最小长度的情况下,该串可以有多少种不同的构成。


思路:最小长度=两个串的长度之和-两个串的LCS。方案数的求法不太会,是看网上的讲解的,还不太明白,先码,以后再说。


#include<bits/stdc++.h>#define N 40using namespace std;typedef long long ll;int main(){    int dp[N][N];    ll ans[N][N];    int t,len1,len;    string s,s1;    cin>>t;    for(int cas=1; cas<=t; cas++)    {        cin>>s>>s1;        len=s.length();        len1=s1.length();        for(int i=0; i<=len; i++) ans[i][0]=1;        for(int i=0; i<=len1; i++) ans[0][i]=1;        memset(dp,0,sizeof(dp));        for(int i=0; i<len; i++)            for(int j=0; j<len1; j++)            {                if(s[i]==s1[j])                {                    dp[i+1][j+1]=dp[i][j]+1;                    ans[i+1][j+1]=ans[i][j];                }                else                {                    if(dp[i+1][j]>dp[i][j+1])                    {                        dp[i+1][j+1]=dp[i+1][j];                        ans[i+1][j+1]=ans[i+1][j];                    }                    else if(dp[i+1][j]<dp[i][j+1])                    {                        dp[i+1][j+1]=dp[i][j+1];                        ans[i+1][j+1]=ans[i][j+1];                    }                    else                    {                        dp[i+1][j+1]=dp[i+1][j];                        ans[i+1][j+1]=ans[i+1][j]+ans[i][j+1];                    }                }            }        printf("Case %d: %d %lld\n",cas,len+len1-dp[len][len1],ans[len][len1]);    }    return 0;}



原创粉丝点击