C - Love Calculator

Description

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 than 30 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

#include<set>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
using namespace std;  
long long dp[65][35][35];  
char s1[110],s2[110];  
int main() 
{
	int r;
	int ca=1;
	scanf("%d",&r);
	while(r--)
	{
	
		scanf("%s%s",s1+1,s2+1);
		int n=strlen(s1+1);
		int m=strlen(s2+1);
		s1[n+1] = '@'; s2[m+1]='@';
		  
		for(int i=0;i<=n+m;i++)              //初始化 
			for(int j=0;j<=n;j++)
			   for(int k=0;k<=m;k++)
			       dp[i][j][k]=0; 
			       
         dp[0][0][0]=1;
         
         for(int i=0;i<=n+m;i++)
           for(int j=0;j<=n;j++)  
             for(int k=0;k<=m;k++)  if(dp[i][j][k])
             {
             	if(s1[j+1]==s2[k+1])        
             	dp[i+1][j+1][k+1]+=dp[i][j][k];
             	
             	else
             	{
	               dp[i+1][j+1][k]+=dp[i][j][k];
				   dp[i+1][j][k+1]+=dp[i][j][k];	
                }
             }
             
        int ans=-1;
		for(int i=0;i<=n+m;i++)       //找出最先掃描完s1,s2的位置且滿足條件的 
		  if(dp[i][n][m])
		  {
  			ans=i;
  			break;
  		}
		printf("Case %d: %d %lld\n",ca++,ans,dp[ans][n][m]);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章