分析:

完整代码:

// 最长公共子序列
#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 ;
}

题型实战:

                1045 Favorite Color Stripe (30分)

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 (≤10​4​​) 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)的更多相关文章

  1. 最长公共子序列(Longest common subsequence)

    问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子序列.(子序列中的字符不要求连续) 这道题可以 ...

  2. UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)

    题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...

  3. 算法实践--最长公共子序列(Longest Common Subsquence)

    什么是最长公共子序列 X=ACCG Y=CCAGCA 长度为1的公共子序列: {A} {C} {G} 长度为2的公共子序列:{AC} {CC} {CG} {AG} 长度为3的公共子序列:{ACG} 长 ...

  4. 动态规划--最长上升子序列(Longest increasing subsequence)

    前面写了最长公共子序列的问题.然后再加上自身对动态规划的理解,真到简单的DP问题很快就解决了.其实只要理解了动态规划的本质,那么再有针对性的去做这方的题目,思路很快就会有了.不错不错~加油 题目描述: ...

  5. nlog(n)解动态规划--最长上升子序列(Longest increasing subsequence)

    最长上升子序列LIS问题属于动态规划的初级问题,用纯动态规划的方法来求解的时间复杂度是O(n^2).但是如果加上二叉搜索的方法,那么时间复杂度可以降到nlog(n).  具体分析参考:http://b ...

  6. 最长公共子串(Longest common substring)

    问题描述: 给定两个序列 X=<x1, x2, ..., xm>, Y<y1, y2, ..., yn>,求X和Y长度最长的公共子串.(子串中的字符要求连续) 这道题和最长公共 ...

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

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 动态规划----最长公共子序列(C++实现)

    最长公共子序列 题目描述:给定两个字符串s1 s2 … sn和t1 t2 … tm .求出这两个字符串的最长公共子序列的长度.字符串s1 s2 … sn的子序列指可以表示为 … { i1 < i ...

  9. 动态规划——最长公共子序列&&最长公共子串

      最长公共子序列(LCS)是一类典型的动归问题. 问题 给定两个序列(整数序列或者字符串)A和B,序列的子序列定义为从序列中按照索引单调增加的顺序取出若干个元素得到的新的序列,比如从序列A中取出 A ...

  10. 动态规划——最长公共子序列(LCS)

    /** * @brief longest common subsequence(LCS) * @author An * @data 2013.8.26 **/ #include <iostrea ...

随机推荐

  1. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

  2. 建造者模式(Builder)——从组装电脑开始

    建造者模式(Builder)--从组装电脑开始 建造者模式概括起来就是将不同独立的组件按照一定的条件组合起来构成一个相对业务完整的对象.调用者无需知道构造的过程. 我们从组装电脑开始 让我们从买组装电 ...

  3. bootstrap234的ie兼容选择

    如果你需要兼容IE8甚至是IE7和IE6,那么只能选择Bootstrap2,虽然它自身在IE6的效果也并不完美.如果需要兼容IE678的话用2.如果需要高版本的浏览器,并且移动端优先的话,那么用boo ...

  4. 转:Flutter开发中踩过的坑

    记录一下入手Flutter后实际开发中踩过的一些坑,这些坑希望后来者踩的越少越好.本文章默认读者已经掌握Flutter初步开发基础. 坑1问题:在debug模式下,App启动第一个页面会很慢,甚至是黑 ...

  5. 字符串相等 impossible 1480

    题目描述 两个长度不超过80的字符串,串中包括大写.小写和空格字符,去掉空格并忽略大小写后,判断两个字符串是否相等. 输入描述 第1行输入是测试数据的组数n,每组测试数据占两行,第1行是第一个字符串s ...

  6. CSS实现同一行中图片和文本垂直居中

    1.为图片和文本都设置vertical-align:middle

  7. 组件使用v-model、$listeners、.sync(区别于v-model的双向数据绑定)

    自定义组件 自定义组件的v-model 首先我们先说一下在自定义组件中使用v-model的必要条件 在自定义的组件中要有input(这里我们先不讨论单选复选框) 在自定义组件的模板对象中要有props ...

  8. Tensor的合并与分割

    先来看一下有哪些接口用来进行张量的合并与分割: tf.concat用来进行张量的拼接,tf.stack用来进行张量的堆叠,tf.split用来进行张量的分割,tf.unstack是tf.split的一 ...

  9. Linux环境搭建及基础操作

    一.Linux环境搭建 1.安装虚拟机软件(VMWare,Parallel) 虚拟机的作用:将本来不是适合当前操作系统的分区虚拟化成适合当前操作系统的分区格式 2.新建虚拟机: 类似买了一台新的电脑, ...

  10. PAT (Basic Level) Practice (中文)1082 射击比赛 (20 分)

    本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军:谁差得最远,谁就是菜鸟.本题给出一系列弹洞的平面坐标(x,y),请你编写程序找出冠军和菜鸟.我们假设靶心在原点(0,0). 输入 ...