分析:

完整代码:

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

    题意简述:有n个点,每一个点都有一个权值,然后有m个条件,每一个条件是a[x]!=a[y],让选择最少的点且至少选择1个,然后让这个点的权值+1,使得条件仍满足 所有数对k取模 题解:如果a[x]+1 ...

  2. 【终端命令】组管理 和 Ubuntu中的"sudo"命令

    一.超级用户root 1.超级用户和标准用户 Linux系统中的root账户通常 用于系统的维护和管理,对操作系统的 所有资源 具有所有访问权限. 在大多数版本的Linux系统中,都 不推荐 直接使用 ...

  3. Python3 协程相关 - 学习笔记

    什么是协程 协程的优势 Python3中的协程 生成器 yield/send yield + send(利用生成器实现协程) 协程的四个状态 协程终止 @asyncio.coroutine和yield ...

  4. tensorflow 中的L1和L2正则化

    import tensorflow as tf weights = tf.constant([[1.0, -2.0],[-3.0 , 4.0]]) >>> sess.run(tf.c ...

  5. POJ 2431 Expedition 贪心 优先级队列

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30702   Accepted: 8457 Descr ...

  6. TChart-数据编辑(TChartListBox,TeeCommander)

    先上图 功能代码: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Contr ...

  7. Json与Ajax(注册实例)

    需要在服务器上进行哈 jquery的ajax方法: // jquery请求 $.ajax({ url: "./server/slider.json", type: "po ...

  8. hdu 2187 悼念512汶川大地震遇难同胞——老人是真饿了(贪心)

    新人题:n2的排序就可以过 #include <stdio.h> #include <stdlib.h> int main() { int c,n,i,j,o; ],b[],m ...

  9. SurfaceView 与view区别详解

    SurfaceView 与view区别详解 https://blog.csdn.net/u011339364/article/details/83347109 2018年10月24日 17:20:08 ...

  10. ISCC2018 Reverse & Pwn writeup

    Reference:L1B0 Re RSA256 春秋欢乐赛原题..flag都不变的 给了三个加密文件和公钥证书public.key,可以使用openssl进行处理 $openssl rsa -pub ...