动态规划 ---- 最长公共子序列(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 ...
随机推荐
- C#画图之饼图
public JsonResult DrawPie() { // 预置颜色 List<Color> colors = new List<Color>() { Color.Fro ...
- idea中创建maven的Javaweb工程并进行配置
学完maven后,可以创建maven的javaweb工程,在创建完成后还需要一些配置,下面来说下具体步骤,在这里我创建的是一个模块,创建web项目的方式和创建模块一样 1.创建一个模块,点new-Mo ...
- Kali Linux中Chrome浏览器不能启动的问题
kali中自带了Chromium Web Browser,我点了几次没反应.我还以为是Chrome的版本问题.于是下载了Chrome的deb包. 安装中还解决了一个包依赖问题.安装成功还是不能启动.于 ...
- Pikachu-Unsafe Fileupload(不安全的文件上传)
不安全的文件上传漏洞概述 文件上传功能在web应用系统很常见,比如很多网站注册的时候需要上传头像.上传附件等等.当用户点击上传按钮后,后台会对上传的文件进行判断 比如是否是指定的类型.后缀名.大小等等 ...
- 任意指定一个key获取该key所处在哪个node节点
需求:任意指定一个key获取该key所处在哪个node节点上. 说明:redis自带的命令可以知道一个key所属的slot,可以知道node master对应哪些slot,但没有key和node的对应 ...
- js微信禁用右上角的分享按钮,和vue中微信页面禁用右上角的分享按钮的问题
1.隐藏微信网页右上角的按钮 document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { // 通过下面这个 ...
- 1级搭建类109-Oracle 12cR2 SI FS(Windows Server 2019)公开
Oracle 12cR2 单实例文件系统在Windows Server 2019上的安装 在线查看
- nat123+nginx实现外网访问本机IIS发布的系统
故事开端(前因) 嗯,内网其实是校园网络,服务器呢,不是阿里云.腾讯云之类的云服务器,而是自己正在码字的笔记本电脑:有公网IP吗?没有!校园IP分配的IP固定不?不固定,动态分配的,额~~~. 我想想 ...
- VMware安装centos7与配置网络
自己想搭建个虚机学习下k8s,使用VMware安装centos7,上不了网,折腾了很久才连上.发现网上很多教程都是错误的或者不明确的,这边写下自己的配置记录 首先安装centos7系统就不赘述了,这边 ...
- 一个扩展搜索API的优化过程
概述 API 是一个服务的门面,就像衣装是人的形象一样. 优雅的 API 设计,能让业务方使用起来倍儿爽,提升开发效率,降低维护成本:糟糕的 API 设计,则让业务方遭心,陷入混沌. 本文将展示一个扩 ...