码迷,mamicode.com
首页 > 其他好文 > 详细

DP [light oj 1013] Love Calculator

时间:2014-12-30 00:29:54      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

1013 - Love Calculator

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

 

 

 

 

 

 

 

 

 

 

做了这个题、只剩下泪了。

最开始记忆化搜索,不造哪里错了Wa。

然后DP、然后把l2写成了12,恰好样例又过了,Wa,各种测试,还以为编译器出问题了

然后改之,由于最开始把数组开大了,超内存,然后改小,然后不造怎么的,数组一变小就输出一串莫名其妙的数字,Ac后才发现好像是中途数组下标为-1越界的原因。

然后改之,由于初始化看错了,过了讨论里所有数据,Wa在第一组,输出6 2。

最后改之,Ac。

受不了,- -

我是二货!

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 0x7fffffff
#define ll long long

ll l1,l2;
char s1[33];
char s2[33];
ll dp[33][33][66];  //dp[i][j][k]表示当长度为k时,包含第1个串中前i个字符,第2个串中前j个字符的方案数。

int main()
{
    ll i,j,k,T,iCase=1;
    scanf("%lld",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%s%s",s1+1,s2+1);
        l1=strlen(s1+1);
        l2=strlen(s2+1);
        for(i=0;i<=l1;i++) dp[i][0][i]=1;
        for(j=0;j<=l2;j++) dp[0][j][j]=1;  //注意初始化细节
        for(k=1;k<=l1+l2;k++)
        {
            for(i=1;i<=l1;i++)
            {
                for(j=1;j<=l2;j++)
                {
                    if(s1[i]==s2[j])
                    {
                        dp[i][j][k]+=dp[i-1][j-1][k-1];
                    }
                    else
                    {
                        dp[i][j][k]+=dp[i-1][j][k-1]+dp[i][j-1][k-1];
                    }
                }
            }
        }
        for(k=1;k<=l1+l2;k++)
        {
            if(dp[l1][l2][k])
            {
                break;
            }
        }
        printf("Case %lld: %lld %lld\n",iCase++,k,dp[l1][l2][k]);
    }
    return 0;
}

 

DP [light oj 1013] Love Calculator

标签:

原文地址:http://www.cnblogs.com/hate13/p/4192616.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!