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

分析:


完整代码:
// 最长公共子序列
#include <stdio.h>
#include <algorithm>
using namespace std; const int N = ;
char A[N], B[N];
int dp[N][N]; int main()
{
freopen("in.txt", "r", stdin);
int n;
gets(A + ); // 从下标1开始读入
gets(B + );
int lenA = strlen(A + ); // 由于读入时下标从1开始,因此读取长度也从1开始
int lenB = strlen(B + ); // 边界
for (int i = ; i <= lenA; i++){
dp[i][] = ;
}
for (int j = ; j <= lenB; j++){
dp[][j] = ;
} // 状态转移方程
for (int i = ; i <= lenA; i++){
for (int j = ; j <= lenB; j++){
if (A[i] == B[j]){
dp[i][j] = dp[i - ][j - ] + ;
}
else{
dp[i][j] = max(dp[i - ][j], dp[i][j - ]);
}
}
}
// dp[lenA][lenB]是答案
printf("%d\n", dp[lenA][lenB]);
fclose(stdin);
return ;
}
题型实战:
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.
It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.
Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤200) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (≤200) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤104) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a separated by a space.
Output Specification:
For each test case, simply print in a line the maximum length of Eva's favorite stripe.
Sample Input:
6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6
Sample Output:
7
分析:

完整代码:
#include <stdio.h>
#include <algorithm>
using namespace std; const int maxc = ; // 颜色的最大种类数
const int maxn = ; // 颜色序列的最大长度
int A[maxc], B[maxn], dp[maxc][maxc]; int main()
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++){
scanf("%d", &A[i]);
}
int L;
scanf("%d", &L);
for (int i = ; i <= L; i++){
scanf("%d", &B[i]);
}
// 边界
for (int i = ; i <= m; i++){
dp[i][] = ;
}
for (int j = ; j <= L; j++){
dp[][j] = ;
} // 状态转移方程
for (int i = ; i <= m; i++){
for (int j = ; j <= L; j++){
// 取dp[i - 1][j]、dp[i][j - 1]中的较大值
int Max = max(dp[i - ][j], dp[i][j - ]);
if (A[i] == B[j]){
dp[i][j] = Max + ;
}
else{
dp[i][j] = Max;
}
}
} // 输出答案
printf("%d\n", dp[m][L]); return ;
}
动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)的更多相关文章
- 最长公共子序列(Longest common subsequence)
问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子序列.(子序列中的字符不要求连续) 这道题可以 ...
- UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)
题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...
- 算法实践--最长公共子序列(Longest Common Subsquence)
什么是最长公共子序列 X=ACCG Y=CCAGCA 长度为1的公共子序列: {A} {C} {G} 长度为2的公共子序列:{AC} {CC} {CG} {AG} 长度为3的公共子序列:{ACG} 长 ...
- 动态规划--最长上升子序列(Longest increasing subsequence)
前面写了最长公共子序列的问题.然后再加上自身对动态规划的理解,真到简单的DP问题很快就解决了.其实只要理解了动态规划的本质,那么再有针对性的去做这方的题目,思路很快就会有了.不错不错~加油 题目描述: ...
- nlog(n)解动态规划--最长上升子序列(Longest increasing subsequence)
最长上升子序列LIS问题属于动态规划的初级问题,用纯动态规划的方法来求解的时间复杂度是O(n^2).但是如果加上二叉搜索的方法,那么时间复杂度可以降到nlog(n). 具体分析参考:http://b ...
- 最长公共子串(Longest common substring)
问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子串.(子串中的字符要求连续) 这道题和最长公共 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 动态规划----最长公共子序列(C++实现)
最长公共子序列 题目描述:给定两个字符串s1 s2 … sn和t1 t2 … tm .求出这两个字符串的最长公共子序列的长度.字符串s1 s2 … sn的子序列指可以表示为 … { i1 < i ...
- 动态规划——最长公共子序列&&最长公共子串
最长公共子序列(LCS)是一类典型的动归问题. 问题 给定两个序列(整数序列或者字符串)A和B,序列的子序列定义为从序列中按照索引单调增加的顺序取出若干个元素得到的新的序列,比如从序列A中取出 A ...
- 动态规划——最长公共子序列(LCS)
/** * @brief longest common subsequence(LCS) * @author An * @data 2013.8.26 **/ #include <iostrea ...
随机推荐
- 建立基于docker的编译环境
如果我们要在一台开发主机上搭一个编译环境,我们需要安装一堆依赖库和编译工具.如果我们有多个不同的项目同时进行,这些项目的编译工具和依赖库又都不一样,如果我们把这些东西全都塞到一台机器里,会不会有冲突呢 ...
- ASP.NET Learning Center---学习ASP.NET(1)
1,学习地址---微软官网 2,目标:学习Razor,以及建立Web应用. 3,https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor ...
- java设计模式学习笔记--浅谈设计模式
设计模式的目的 编写软件的过程中,程序员面临着来自耦合性,内聚性以及可维护性,可扩展性,重用性,灵活性等多方面的挑战.设计模式为了让程序具有更好的 1.代码重用性(即:相同功能的代码,不用多次编写) ...
- python神器pycharm的安装
1.下载PyCharm 官网下载:http://www.jetbrains.com/pycharm/download/#section=windows百度网盘下载:https://pan.baidu. ...
- 【46】谷歌 Inception 网络简介Inception(2)
Inception 网络(Inception network) 在上节笔记中,你已经见到了所有的Inception网络基础模块.在本节笔记中,我们将学习如何将这些模块组合起来,构筑你自己的Incept ...
- Reg文件操作
注册表REG脚本文件测试 1.新建主键 例如,想在主键[HKEY_CURRENT_USER\Software]下新建一个名叫“新建主键名称”的主键. 可以打开记事本,写入如下内容: Windows R ...
- Java高级项目实战之CRM系统01:CRM系统概念和分类、企业项目开发流程
1. CRM系统介绍 CRM系统即客户关系管理系统, 顾名思义就是管理公司与客户之间的关系. 是一种以"客户关系一对一理论"为基础,旨在改善企业与客户之间关系的新型管理机制.客户关 ...
- STL-list 链表
#include <iostream> #include <list> using namespace std; int main() { // list可以在头部和尾部插入和 ...
- Java-公约公倍
题目: 如果两个数很大,怎样求最大公约数,最小公倍数?如果是n个数呢?比如1000个数的最小公倍数 分析:求a和b的最大公约数——辗转相除法(又叫欧几里得定理).即找到一个数,能对a,b都除尽.对于这 ...
- CentOS 7 部署 Redis(单机版)
一.部署环境说明 软件 版本 安装包 CentOS 7.2 CentOS 7.2 Redis 4.0.14 redis-4.0.14.tar.gz 二.开始部署 安装gcc依赖 [root@bmsof ...