<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 ...
随机推荐
- 【大话QT之十】实现FTP断点续传
应用需求: 网盘开发工作逐步进入各部分的整合阶段,当用户在client改动或新添加一个文件时.该文件要同步上传到server端相应的用户文件夹下,因此针对传输数据(即:上传.下载)这一块如今既定了三种 ...
- sql server2008对字符串日期字段分区
近期对公司产品的日志数据库做了一个数据分区,数据库使用的是sql server 2008,这里给大家提供一个參考. 须要特别说明的是,非常多网上的样例分区字段都使用的是时间类型的.而这里因为时间字段原 ...
- oracle实现自增id
--oracle实现自增id --创建一张T_StudentInfo表 create table T_StudentInfo ( "id" integer not null pri ...
- node--19 moogose demo1
db.js /** * Created by Danny on 2015/9/28 16:44. */ //引包 var mongoose = require('mongoose'); //创建数据库 ...
- [codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)
题目链接:http://codeforces.com/problemset/problem/894/E 题目大意: $n$个点$m$条边的有向图,每条边有一个权值,可以重复走. 第$i$次走过某条边权 ...
- xBIM 高级03 更改日志创建
系列目录 [已更新最新开发文章,点击查看详细] 模型中发生的每一个变化都是事务的一部分,这是我们设计的核心.所有事务都是由 IModel 的实现创建的,并且从中被弱引用,因此当使用 using ...
- c#0218-命名空间
1 namespace 命名空间 可以解决类的重命名问题 可以看做是类的文件夹: 2 跨项目使用类 一个解决方案下有不同的项目,如果想在一个项目中引用另一个项目的类,解决方法是 1 添加引用 2 引用 ...
- web监控脚本
#!/bin/bashvalues=`curl -H 'Cache-Control: no-cache' -k -s -m 10 --connect-timeout 10 "$1" ...
- ES6学习笔记(二十)Module 的加载实现
上一章介绍了模块的语法,本章介绍如何在浏览器和 Node 之中加载 ES6 模块,以及实际开发中经常遇到的一些问题(比如循环加载). 1.浏览器加载 传统方法 HTML 网页中,浏览器通过<sc ...
- 【jQuery05】通过按键 来切换 class
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...