<Sicily> Longest Common Subsequence
一、题目描述
Given a sequence A = < a1, a2, …, am >, let sequence B = < b1, b2, …, bk > be a subsequence of A if there exists a strictly increasing sequence ( i1 < i2 < i3 …, ik ) of indices of A such that for all j = 1,2,…,k, aij = bj. For example, B = < a, b, c, d > is a subsequence of A= < a, b, c, f, d, c > with index sequence < 1, 2, 3 ,5 >。 
Given two sequences X and Y, you need to find the length of the longest common subsequence of X and Y.
二、输入
The input may contain several test cases.
The first line of each test case contains two integers N (the length of X) and M(the length of Y), The second line contains the sequence X, the third line contains the sequence Y, X and Y will be composed only from lowercase letters. (1<=N, M<=100)
Input is terminated by EOF.
三、输出
Output the length of the longest common subsequence of X and Y on a single line for each test case. 
例如: 
输入: 
6 4 
abcfdc 
abcd 
2 2 
ab 
cd 
输出: 
4 
0
四、解题思路
这道题需要求的是最长公共子序列,典型的动态规划问题。 
设序列1:X = < x1,  x2,  x3, …, xm>,子序列2:Y=< y1, y2, y3,…yn>。假如他们的最长公共子序列为Z=< z1, z2, z3,…zk>那么k就是我们需要求的长度。 
由上面假设可以推出: 
1)如果xm=yn,那么必有xm=yn=zk,且< x1,x2,x3,…xm-1>与< y1,y2,y3,…yn-1>的最长公共子序列为< z1, z2, z3,…zk-1>
2)如果xm!=zk,那么< z1, z2, z3,…zk>是< x1,x2,x3,…xm-1>与< y1, y2, y3,…yn>的最长公共子序列。
3)如果yn!=zk,那么< z1, z2, z3,…zk>是< x1,x2,x3,…xm>与< y1, y2, y3,…yn-1>的最长公共子序列。
由此可以逆推。于是有以下公式:
五、代码
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int strALeng, strBLeng;
    while(cin >> strALeng >> strBLeng)
    {
        int charMatrix[101][101];
        char charAAry[strALeng];
        char charBAry[strBLeng];
        for(int i = 0; i < strALeng; i++)
            cin >> charAAry[i];
        for(int i = 0; i < strBLeng; i++)
            cin >> charBAry[i];
        for(int i = 0; i < strALeng; i++)
            charMatrix[i][0] = 0;
        for(int i = 0; i < strBLeng; i++)
            charMatrix[0][i] = 0;
        for(int i = 1; i <= strALeng; i++)
        {
            for(int j = 1; j <= strBLeng; j++)
            {
                if(charAAry[i - 1] == charBAry[j - 1]) charMatrix[i][j] = charMatrix[i - 1][j - 1] + 1;
                else charMatrix[i][j] = max(charMatrix[i][j-1], charMatrix[i - 1][j]);
            }
        }
        cout << charMatrix[strALeng][strBLeng] << endl;
    }
    return 0;
}
<Sicily> Longest Common Subsequence的更多相关文章
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
		1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ... 
- LintCode Longest Common Subsequence
		原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ... 
- [UCSD白板题] Longest Common Subsequence of Three Sequences
		Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ... 
- LCS(Longest Common Subsequence 最长公共子序列)
		最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ... 
- Longest Common Subsequence
		Given two strings, find the longest common subsequence (LCS). Your code should return the length of ... 
- Longest Common Subsequence & Substring & prefix
		Given two strings, find the longest common subsequence (LCS). Your code should return the length of ... 
- Dynamic Programming | Set 4 (Longest Common Subsequence)
		首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ... 
- Lintcode:Longest Common Subsequence  解题报告
		Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ... 
- UVA 10405 Longest Common Subsequence (dp + LCS)
		Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ... 
- [HackerRank] The Longest Common Subsequence
		This is the classic LCS problem. Since it requires you to print one longest common subsequence, just ... 
随机推荐
- angular4(2-2)angular脚手架引入第三方类库(swiper)
			试了好多方法,npm install 方法失败了,下载到本地是可以使用的: 将swiper文件放到assets文件下: 项目目录下:(命令行) 因为ts并不能准确识别js语法,所以需要用ts中的int ... 
- Volatile variables
			Volatile variables apply another type of memory constraint to individual variables. The compiler oft ... 
- vue 所有的路由跳转加一个统一参数
			需求是什么 所有的路由跳转加一个统一的参数 实现方式 先深入理解一下router的全局前置守卫 router.beforeEach((to, from, next) => { const que ... 
- linux 杀掉端口
			netstat -apn|grep 8184 tcp 0 0 0.0.0.0:8184 0.0.0.0:* LISTEN ... 
- JMS消息
			1.消息可分为3部分:消息头.属性和有效负载 消息头:用于标识消息.声明消息属性及提供路由信息的特殊字段组成. 消息的属性区包含了和该消息有关的附加元数据,这个元数据由应用程序开发者进行设置,或者由J ... 
- yum-config-manager --add-repo=
			[root@server0 yum.repos.d]# yum-config-manager --add-repo=ftp://192.168.31.121/centos7u4Loaded plugi ... 
- 紫书 习题 8-25 UVa 11175 (结论证明)(配图)
			看了这篇博客https://blog.csdn.net/u013520118/article/details/48032599 但是这篇里面没有写结论的证明, 我来证明一下. 首先结论是对于E图而言, ... 
- 紫书 习题 8-20 UVa 1620 (找规律+求逆序对)
			这道题看了半天没看出什么规律, 然后看到别人的博客, 结论是当n为奇数且逆序数为奇数的时候 无解, 否则有解.但是没有给出证明, 在网上也找到详细的证明--我也不知道是为什么-- 求逆序对有两种方法, ... 
- 紫书 习题8-9 UVa 1613 (dfs染色+图的性质)
			这道题一开始我没想什么直接开始染, 但是是for循环一个节点一个节点染, 然后就WA 后了看了https://www.cnblogs.com/jerryRey/p/4702323.html 发现原来还 ... 
- HDU 3046 Pleasant sheep and big big wolf
			Pleasant sheep and big big wolf Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged ... 
