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

题目大意:
给你两个串, 让你求出这两个串能组成的最短串的长度,以及最短串多少种不同的串(第三个串中保证有最长公共字串)
 
最长公共子序列, 在求最长公共子序列的过程中加上每个串有多少种组成方法
 
dp[i][j] 代表第一个串的前i个和第二个串的前j个的最长公共子序列
num[i][j]代表第一个串的前i个和第二个串的钱j个能组成多少种不同的串
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std; #define N 1100 #define met(a,b) (memset(a,b,sizeof(a)))
typedef long long LL; int a[][], dp[][];
LL num[][]; int main()
{
int T, iCase=; scanf("%d", &T); while(T--)
{
char s1[], s2[];
int i, j, len1, len2; met(dp, );
met(num, );
scanf("%s%s", s1, s2);
len1 = strlen(s1);
len2 = strlen(s2); for(i=; i<=len1; i++)
num[i][] = ;
for(i=; i<=len2; i++)
num[][i] = ; for(i=; i<=len1; i++)
for(j=; j<=len2; j++)
{
if(s1[i-]==s2[j-])
{
dp[i][j] = dp[i-][j-] + ;
num[i][j] += num[i-][j-];
}
else
{
if(dp[i-][j]>dp[i][j-])
{
dp[i][j] = dp[i-][j];
num[i][j] = num[i-][j];
}
else if(dp[i-][j]<dp[i][j-])
{
dp[i][j] = dp[i][j-];
num[i][j] = num[i][j-];
}
else
{
dp[i][j] = dp[i-][j];
num[i][j] = num[i-][j] + num[i][j-];
}
}
} printf("Case %d: %d %lld\n", iCase++, len1+len2-dp[len1][len2], num[len1][len2]);
}
return ;
} /**
3
USA
USSR
LAILI
MAJNU
SHAHJAHAN
MOMTAJ
*/

自己写完后, 搜题解看到的另一种写法, 由于dp刚入门, 就学习一下思想

dp[C串的长度][包含A的字符个数][包含B的字符个数] = 种类数

状态转移:如果 A[i] == B[j] 那么 dp[k][i][j] = dp[k-1][i-1][j-1]. 就是说我最后一个字符是相同的那么我只要放一个就可以了。

     如果 A[i] !=  B[j] 那么 dp[k][i][j] = dp[k-1][i-1][j] + dp[k-1][i][j-1].最后一个字符我们要么放A[i] 要么放 B[j] 就这两种情况了。

然后关于找最短的,就可以在 dp[k][lenA][lenB] 种找到最小的k即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm> using namespace std; #define N 1100 #define met(a,b) (memset(a,b,sizeof(a)))
typedef long long LL; LL dp[][][]; int main()
{
int T, iCase=; scanf("%d", &T); while(T--)
{
char s1[], s2[];
int i, j, k, len1, len2; met(dp, );
scanf("%s%s", s1, s2);
len1 = strlen(s1);
len2 = strlen(s2); for(i=; i<=len1; i++)
dp[i][i][] = ;
for(i=; i<=len2; i++)
dp[i][][i] = ; for(i=; i<=len1+len2; i++)
{
for(j=; j<=len1; j++)
for(k=; k<=len2; k++)
{
if(s1[j-]==s2[k-])
dp[i][j][k] = dp[i-][j-][k-];
else
dp[i][j][k] = dp[i-][j-][k] + dp[i-][j][k-];
}
} for(k=; k<=len1+len2; k++)
if(dp[k][len1][len2]) break; printf("Case %d: %d %lld\n", iCase++, k, dp[k][len1][len2]);
}
return ;
} /**
3
USA
USSR
LAILI
MAJNU
SHAHJAHAN
MOMTAJ
*/

(最长公共子序列+推导)Love Calculator (lightOJ 1013)的更多相关文章

  1. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  2. 最长公共子序列与最长公共字串 (dp)转载http://blog.csdn.net/u012102306/article/details/53184446

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  3. 算法练习——最长公共子序列的问题(LCS)

    问题描述: 对于两个序列X和Y的公共子序列中,长度最长的那个,定义为X和Y的最长公共子序列.X  Y   各自字符串有顺序,但是不一定需要相邻. 最长公共子串(Longest Common Subst ...

  4. DP_最长公共子序列/动规入门

    学自:https://open.163.com/movie/2010/12/L/4/M6UTT5U0I_M6V2U1HL4.html 最长公共子序列:(本文先谈如何求出最长公共子序列的长度,求出最长公 ...

  5. 题解报告:hdu 1159 Common Subsequence(最长公共子序列LCS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Problem Description 给定序列的子序列是给定的序列,其中有一些元素(可能没有) ...

  6. DP的初级问题——01包、最长公共子序列、完全背包、01包value、多重部分和、最长上升子序列、划分数问题、多重集组合数

    当初学者最开始学习 dp 的时候往往接触的是一大堆的 背包 dp 问题, 那么我们在这里就不妨讨论一下常见的几种背包的 dp 问题: 初级的时候背包 dp 就完全相当于BFS DFS 进行搜索之后的记 ...

  7. 【转】动态规划之最长公共子序列(LCS)

    [原文链接]最长公共子序列(Longest Common Subsequence,简称 LCS)是一道非常经典的面试题目,因为它的解法是典型的二维动态规划,大部分比较困难的字符串问题都和这个问题一个套 ...

  8. 【转】最长公共子序列(LCS),求LCS长度和打印输出LCS

    求LCS的长度,Java版本: public static int LCS(int[]a,int[] b) { int [][]c=new int[a.length+1][b.length+1]; f ...

  9. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

随机推荐

  1. chrome控制台查看控件有没绑定事件[转]

    chrome控制台查看btn_comment_submit控件有没绑定事件 function lookEvents (elem) {     return $.data ? $.data( elem, ...

  2. 纸上谈兵:伸展树(splay tree)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们讨论过,树的搜索效率与树的深度有关.二叉搜索树的深度可能为n,这种情况下,每次 ...

  3. mysql的sql_mode 模式修改 my.cnf

    1. sql_mode模式 mysql数据库的中有一个环境变量sql_mode,定义了mysql应该支持的sql语法,数据校验等!我们可以通过以下方式查看当前数据库使用的sql_mode: mysql ...

  4. c++学习笔记——字面值常量类

    字面值常量类:数据成员都是字面值类型的聚合类是字面值常量类.如果一个类不是聚合类,但是它符合一下要求,则它也是个字面值常量类: 1.数据成员都必须是字面值类型. 2.类必须至少含有一个constexp ...

  5. afx , afxMessageBox , MessageBox

    afx开头的是全局函数,可以在任何地方使用 MessageBox是CWnd的子函数,只能在CWnd窗口类对象里面用, AfxMessageBox的函数原型 int AfxMessageBox( LPC ...

  6. 怎么提高ArcSDE 写入地理数据库的效率

    link: http://blog.csdn.net/linghe301/article/details/20900615 2014-03-14 09:20 2686人阅读 评论(6) 收藏 举报   ...

  7. 如何在A用户下建立视图,这个视图是A的表与B的表进行关联的?

    这个前提条件是,同一个数据库,不同用户!!!如果是不同数据库,就要用dblink了 一开始,我直接创建视图,但是提示“权限不足”: 于是我是用A登陆,直接用select * from B.sa_tas ...

  8. 理解node模块的exports和module.exports

    exports是module.exports的引用,即var exports = module.exports.在一个模块的开头,这两个值都指向同一个空对象:exports = module.expo ...

  9. OA 办公自动化系统:权限管理模块的实现原理思路

    OA系统分有许多的模块,如系统管理模块.等一些比较高级的业务操作.此类业务是不允许让普通员工来操作的,思路如下: 给系统添加角色表,每个用户对应一个角色,每个角色可以拥有多个权限, 如下:创建权限表( ...

  10. 使用python的subprocess启动windows程序提示WindowsError: [Error 6] The handle is invalid

    代码如下: subp = subprocess.Popen(cwd_path + "test.exe", cwd = cwd_path, shell = True, stdout ...