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. JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)

    链接:JSP+Servlet中使用jspsmartupload.jar进行图片上传下载 关于cos.jar,百度百科只有这么几句话(http://baike.baidu.com/subview/406 ...

  2. C#EXCEL 操作类--C#DataToExcel帮助类

    using System; using System.Diagnostics; //using Excel; namespace DotNet.Utilities {     /// <summ ...

  3. java新项目的eclipse统一配置记录

    1.new java file的模版 /** * @Title:${file_name} * @Copyright: Copyright (c) 2016 * @Description: * < ...

  4. Qlikview List控件

    将纵向展示变为横向展示 方法: ListBox属性分页,“外观”分页“单列”属性不要打钩,用鼠标调整控件高度,Listbox控件会自适应现实将数据打横现实.

  5. jquery事件合集

    1.在input输入数据时执行的事件(边输入边触发事件) $("input[id='subjectNum']").bind('input propertychange', func ...

  6. IDEA 用了maven后的 智能提示 不出现问题,项目的依赖包没有加载依赖库中的问题。

  7. VS2012调试时无法启动程序和拒绝访问问题汇总

    很多人在使用VS2012的时候会出现下面所示的问题,我也是,而且不止一次,也不是同样的问题,我这里就把一些常见的解决方法罗列一下.

  8. 抽象数据类型ADT

    ADT(Abstract Data Type) 类型由什么组成? 一个类型(type)指定两类信息,一个属性集和一个操作集. 假设要定义一个新的数据类型.首先,要提供存储数据的方式,可能是通过设计一个 ...

  9. QT报错Error processing

    执行命令:qmake modbus_ups_mlrl.pro modbus_ups_mlrl.pro文件内容: TEMPLATE = vclib CONFIG +=qt debug thread QT ...

  10. Find the equipment indices

    Here is a simple program test task, it doesn't have very diffcult logic: A zero-indexed array A cons ...