LightOJ-1013 LCS+递推

探讨了如何通过计算两个名字的最短公共子序列来评估它们之间的“爱情百分比”。该算法首先确定最短子序列的长度,然后计算所有可能的唯一最短子序列的数量。
1013 - Love Calculator
Time Limit: 2 second(s)Memory Limit: 32 MB

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

Output for Sample Input

3

USA

USSR

LAILI

MAJNU

SHAHJAHAN

MOMTAJ

Case 1: 5 3

Case 2: 9 40

Case 3: 13 15

 


PROBLEM SETTER: JANE ALAM JAN


题目链接:


题目大意:
  给出两个字符串A、B,求一个包含这两个字符串为子序列的字符串的最小长度,并求出方案数。

解题思路:
  最小长度可以通过求最长公共子序列得到,为len(A)+len(B)-LCS(A,B)。
  难点在于得到方案数,令f[i][j]为取A前i个字符,B前j个字符得到最短字符串的方案数,
则有 dp[i][j-1]>dp[i-1][j]时,f[i][j]=f[i][j-1];
   dp[i][j-1]<dp[i-1][j]时,f[i][j]=f[i-1][j];
   dp[i][j-1]=dp[i-1][j]时,f[i][j]=f[i][j-1]+f[i-1][j]。
  网上还有另一种递推方式,是用f[k][i][j]表示取A前i个字符,B前j个字符构造长度为k的字符串的方案数。 但递推式为
  A[i]==B[j]时,f[k][i][j]=f[k-1][i][j];
  A[i]!=B[j]时,f[k][i][j]=f[k-1][i][j-1]+f[k-1][i-1][j]。
  这样递推对于构造最短串来说方案数是没有问题的,但是非最短串方案数少。
  举例来说,两个字符串AA、DA,f[4][2][2]=0就不正确,但f[3][2][2]=2却是正确的,这是因为f[4][2][2]的上一个状态
f[3][1][1]=0,所以这种递推的定义并不明确。

AC代码:
import java.util.*;

public class Main {
	static int T,n,m,ans;
	static char[] aa,bb;
	static int[][] dp=new int[35][35];
	static long[][] ff=new long[35][35];

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		T=in.nextInt();
		for(int t=1;t<=T;t++)
		{
			aa=(" "+in.next()).toCharArray();
			bb=(" "+in.next()).toCharArray();
			n=aa.length-1;m=bb.length-1;
			for(int i=0;i<=n;i++) ff[i][0]=1;
			for(int i=0;i<=m;i++) ff[0][i]=1;
			for(int i=1;i<=n;i++)
			for(int j=1;j<=m;j++)
			{
				if(aa[i]==bb[j])
				{
					dp[i][j]=dp[i-1][j-1]+1;
					ff[i][j]=ff[i-1][j-1];
				}
				else
				{
					dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
					ff[i][j]=0;
					if(dp[i][j-1]>=dp[i-1][j])
						ff[i][j]+=ff[i][j-1];
					if(dp[i][j-1]<=dp[i-1][j])
						ff[i][j]+=ff[i-1][j];
				}
			}
			ans=n+m-dp[n][m];
			System.out.println("Case "+t+": "+ans+" "+ff[n][m]);
		}
	}
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值