LightOJ1013---Love Calculator (dp)

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

f[i][j]表示第一个串匹配到i,第二个串匹配到j需要的最短长度

dp[i][j]表示f[i][j]长度下的方案数

转移很简单

但是有一个地方要注意:

如果A[i]==B[j],那么方案数计算的时候,只能从f[i?1][j?1]处转移到, 如果从f[i?1][j]f[i][j?1]转移,会重复

/*************************************************************************
    > File Name: LightOJ1013.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年06月07日 星期日 21时41分25秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

string A, B;
LL dp[50][50];
int f[50][50];

int main() {
    int t, icase = 1;
    scanf("%d", &t);
    while (t--) {
        cin >> A >> B;
        int n = A.length();
        int m = B.length();
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        memset(f, inf, sizeof(f));
        f[0][0] = 0;
        for (int i = 1; i <= n; ++i) {
            f[i][0] = i;
            dp[i][0] = 1;
        }
        for (int j = 1; j <= m; ++j) {
            f[0][j] = j;
            dp[0][j] = 1;
        }
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) {
                if (f[i][j] > f[i - 1][j] + 1) {
                    f[i][j] = f[i - 1][j] + 1;
                }
                if (f[i][j] > f[i][j - 1] + 1) {
                    f[i][j] = f[i][j - 1] + 1;
                }
                if (A[i - 1] == B[j - 1]) {
                    if (f[i][j] > f[i - 1][j - 1] + 1) {
                        f[i][j] = f[i - 1][j - 1] + 1;
                    }
                }
                if (f[i][j] == f[i - 1][j] + 1 && A[i - 1] != B[j - 1]) {
                    dp[i][j] += dp[i - 1][j];
                }
                if (f[i][j] == f[i][j - 1] + 1 && A[i - 1] != B[j - 1]) {
                    dp[i][j] += dp[i][j - 1];
                }
                if (A[i - 1] == B[j - 1] && f[i][j] == f[i - 1][j - 1] + 1) {
                    dp[i][j] += dp[i - 1][j - 1];
                }

            }
        }
        printf("Case %d: %d %lld\n", icase++, f[n][m], dp[n][m]);
    }
}
时间: 2024-08-11 09:45:25

LightOJ1013---Love Calculator (dp)的相关文章

LightOJ1013 Love Calculator(DP)

容易猜测到包含s1.s2序列的串的最短长度是LCS(s1,s2) + ( len(s1) - LCS(s1,s2) ) + ( len(s2) - LCS(s1,s2) ) ,即: len(s1)+len(s2)-LCS(s1,s2) 接下来求方案数,可以想到: dp[k][i][j]表示由s1前i位和s2前j位的序列构成的长度为k的串的方案数 dp[k][i][j]是由dp[k-1][i-1][j].dp[k-1][i][j-1]和dp[k-1][i-1][j-1]转移的,而从dp[k-1][

lightoj-1013 - Love Calculator(lcs变体)

1013 - Love Calculator PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBYes, you are developing a 'Love calculator'. The software would be quite complex such that nobody could crack the exact behavior of the software. So, give

DP [light oj 1013] Love Calculator

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' ac

(最长公共子序列+推导)Love Calculator (lightOJ 1013)

http://www.lightoj.com/volume_showproblem.php?problem=1013 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 genera

UVa - 12664 - Interesting Calculator

先上题目: 12664 Interesting CalculatorThere is an interesting calculator. It has 3 rows of button.• Row 1: button 0, 1, 2, 3, . . . , 9. Pressing each button appends that digit to the end of the display.• Row 2: button +0, +1, +2, +3, . . . , +9. Pressin

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

51Nod 1009 数字1的个数 | 数位DP

题意: 小于等于n的所有数中1的出现次数 分析: 数位DP 预处理dp[i][j]存 从1~以j开头的i位数中有几个1,那么转移方程为: if(j == 1) dp[i][j] = dp[i-1][9]*2+pow(10,i-1);else dp[i][j] = dp[i-1][9]+dp[i][j-1]; 然后注意下对于每个询问统计的时候如果当前位为1需要额外加上他后面所有位数的个数,就是n%pow(10,i-1); 这样总复杂度log(n)*10 #include <bits/stdc++.