lightoj 1013

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

 

本题的题意是求一个字符串, 要求字符串a, b是该字符串的子序列, 输出该字符串的最小长度, 并输出有多少种这样的字符串 


这个题本质其实就是LCS(longest comman sequence)的变形,想想看,最终构成的字符串的长度肯定是两个串的长度相加减去lcs的长度,但是有多少的这样的串呢?

初做dp的话还真是挺难想的,不过DP靠的就是多做多练了,感觉嘛,培养培养就有了,考虑这样的状态dp[i][j][k],表示构造了i长度的字符串,利用了第一个串的前i个字符以及第二个字符串的前j个字符,这个状态的意义就是当前状态下总的方案数,那么现在考虑转移,其实我们是要在后面继续扩充一个字符,如果a[j+1] = b[k+1],就可以转移到dp[i+1][j+1][k+1]的状态,

否则,可以转移到dp[i+1][j][k+1]或者dp[i+1][j+1][k];


/*

 这道题我也存在疑问,有兴趣的欢迎大家可以给我留言,探讨

*/


#include <cstdio>
#include <cstring>
typedef long long ll;
ll dp[65][35][35];
char a[35], b[35];
int main()
{
  int t, ca;
  ca = 1;
  scanf("%d", &t);
  getchar();
  while(t--)
     {
       scanf("%s%s", a+1, b+1);
       int n = strlen(a + 1);
       int m = strlen(b + 1);
       int ans;
       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<=m+n; i++)
          for(int j=0; j<=n; j++)
            for(int k=0; k<=m; k++)
              if(dp[i][j][k])
                {
                 if(a[j+1] == b[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];
                   }
                }
        for(int i=1; i<=n+m; i++)
           if(dp[i][n][m])
             {
                ans = i;
                break;
             }
       printf("Case %d: %d %lld\n", ca++, ans, dp[ans][n][m]);
     }
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值