动态规划 ---- 最长公共子序列(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 ...
随机推荐
- UESTC 1324 卿学姐与公主 分块板子
#include<iostream> #include<cmath> using namespace std; ; //表示当前数在哪一块里面 int belong[maxn] ...
- 数据库中间件DBLE学习(二) 学习配置schema.xml
前言 一边有一个经常引诱我让我"娱乐至死"的视频,还有一个不停"鞭策"我让我快点学习的大BOSS.正是有这两种极端的爱才让我常常在自信中明白自己努力的方向.嗯, ...
- openssl CVE-2016-2107 漏洞检测
最近在安装python3 时升级openssl 版本,在摸索openssl 升级过程中才发现centos6 默认安装的openssl 1.0.1e 版本是有一个严重的漏洞的(Padding oracl ...
- 剑指offer-面试题45-把数组排成最小的数-规律
/* 题目: 给定一个int数组,返回数组中各数字排成的最下字符串. */ /* 思路: 比较两个数字之间的先后顺序,谁排在前面更小,从而对数组进行排序,得到结果. 两个数字先后顺序的比较方法:两个数 ...
- 【Android】LitePal的基础
一.环境配置 LitePal 在GitHub地址为:https://github.com/LitePalFramework/LitePal 我们使用起来也很方便,直接在gradle中配置即可. 如果你 ...
- 解决Mac无法写入U盘问题
注:本文出自博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 本文源链接:https://www.cnblogs.com/chloneda/p/upan-to- ...
- Redis的主从复制与Redis Sentinel哨兵机制
1 Redis的主从复制 1.1 什么是主从复制 持久化保证了即使redis服务重启也不会丢失数据,因为redis服务重启后会将硬盘上持久化的数据恢复到内存中,但是当redis服务器的硬盘损 ...
- Yarn报错:Exception message: /bin/bash: line 0: fg: no job control
Exception message: /bin/bash: line 0: fg: no job control 这个错误是 本地idea跨平台远程调试hadoop集群出现的,在使用windows调用 ...
- 安装DHCP到CentOS(YUM)
运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:DHCP-x 硬件要求:无 安装过程 1.安装YUM源,由EPEL提供 [root@localh ...
- PAT (Basic Level) Practice (中文)1021 个位数统计 (15 分)
给定一个 k 位整数 1 (0, ,, dk−1>0),请编写程序统计每种不同的个位数字出现的次数.例如:给定 0,则有 2 个 0,3 个 1,和 1 个 3. 输入格式: 每个输入包含 ...