目录

Common Subsequence

题目解释:

解题思路:

ac代码:


Common Subsequence

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) : 26   Accepted Submission(s) : 15

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y. 
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

Sample Input

abcfbc abfcab
programming contest
abcd mnp

Sample Output

4
2
0

题目解释:


求两个字符串的最长公共子序列的长度(子序列可以不连续)

解题思路:


状态转移方程:

if (a[i]==b[j])

dp[i][j]=dp[i-1][j-1]+1;

else

dp[i][j]=max(dp[i-1][j],dp[i][j-1]);

ac代码:


注意字符串的读取语句

#include <iostream>#include <cstring>
#define maxn 1005
using namespace std;
int main()
{int dp[maxn][maxn];//dp[i][j]表示a的第i个字符和b的第j个字符的LCSchar a[maxn],b[maxn];while(cin>>a+1>>b+1){int lena=strlen(a+1),lenb=strlen(b+1);int i,j;//i指向a,j指向bfor(i=0;i<=lena;i++)//i从0开始,因为下面有i-1dp[i][0]=0;for(j=0;j<=lenb;j++)dp[0][j]=0;for(i=1;i<=lena;i++){for(j=1;j<=lenb;j++){if(a[i]==b[j])dp[i][j]=dp[i-1][j-1]+1;elsedp[i][j]=max(dp[i-1][j],dp[i][j-1]);}}printf("%d\n",dp[lena][lenb]);}return 0;
}

hdoj1159:Common Subsequence(dp基础题-最长公共子序列LCS)相关推荐

  1. [dp]leetcode1143:最长公共子序列LCS (medium)

    题目: 题解: 动态规划的经典例题,可参考晴神的算法笔记 首先先使用暴力法思考吧,设t1和t2的长度分别为m和n,那么对两个字符串中的每个字符,分别只有选和不选两个决策,而得到两个子序列后,比较两个子 ...

  2. 经典算法题——最长公共子序列

    ** 解析: ** 此题一共有两个要点: 1.求上述两个最长公共子序列的长度 2.求所有可能出现的最长公共子序列个数,答案可能很大,只要将答案对10^8求余即可 第一个都很好想到,难点在于第二个.下面 ...

  3. 最长公共子序列 (LCS) 详解+例题模板(全)

    欢迎访问https://blog.csdn.net/lxt_Lucia-- 宇宙第一小仙女\(^o^)/-萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗- ------------ ...

  4. 动态规划算法解最长公共子序列LCS问题

    动态规划算法解LCS问题 作者 July 二零一零年十二月三十一日 本文参考:微软面试100题系列V0.1版第19.56题.算法导论.维基百科. 第一部分.什么是动态规划算法 ok,咱们先来了解下什么 ...

  5. 算法之最长公共子序列(LCS)问题

    算法课上老师留的作业,最长公共子序列LCS(Longest Common Subsequence)问题,首先看到这个问题感觉有点复杂,和最长公共子串不同,公共子序列并不要求元素相邻,看起来只有穷举才能 ...

  6. 动态规划之最长公共子序列(LCS)

    最长公共子序列(LCS,Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已知序列的最 ...

  7. 程序员编程艺术第十一章:最长公共子序列(LCS)问题

    程序员编程艺术第十一章:最长公共子序列(LCS)问题 0.前言 程序员编程艺术系列重新开始创作了(前十章,请参考程序员编程艺术第一~十章集锦与总结).回顾之前的前十章,有些代码是值得商榷的,因当时的代 ...

  8. 算法导论-----最长公共子序列LCS(动态规划)

    目录 一.概念梳理 二.最长公共子序列解决方案 方案1:蛮力搜索策略 方案2:动态规划策略 三.C代码实现 实现1 实现2(空间优化) 一.概念梳理   1. 子序列(subsequence): 一个 ...

  9. 动态规划表格法解决最长公共子序列(LCS)问题

    3.5 最长公共子序列(LCS) 前言:图片是博主自己画的,转载请注明出处哦 3.5.1 问题描述 最长公共子序列(Longest Common Subseuence,LCS)问题:给定两个字符串,求 ...

  10. python实现求解最长公共子序列LCS问题

    在实现论文<Automatically Generating Models for Botnet Detection>论文的算法中,用到了一个The longest commom subs ...

最新文章

  1. Python那些优雅的写法:switch-case
  2. [学习OpenCV攻略][001][Ubuntu安装及配置]
  3. 【LeetCode】【字符串】题号:*58. 最后一个单词的长度
  4. LINUX修改.bashrc之后,生效的办法
  5. (转)孙正义:数字资产会成为人类最大的资产
  6. 数据结构题集c语言版答案严蔚敏第二章,数据结构习题集答案(C语言版严蔚敏)2(可编辑).doc...
  7. 墨者学院—网络安全篇3
  8. python的request返回400_爬虫发出ajax请求,requests能获取正常响应,scrapy发出请求却返回400...
  9. python subprocess.Popen 监控控制台输出
  10. 2007年五一通过了驾驶证考试(5/7,5/8)
  11. android 4.4 短信拦截,Android 4.4 KitKat升级率已经接近18%
  12. 语音合成:transformer tts 论文复现以及dockerfile
  13. CF1463F Max Correct Set(取小样法+状压 DP)
  14. mysql性能优化与高可用_MySQL管理之道:性能调优、高可用与监控》迷你书
  15. micro:bit 了解
  16. c++实验3—定期存款利息计算器
  17. 攻击入侵检测NIDS分析
  18. new115.com dz.html,人源血管紧张素转化酶-C结构域在毕赤酵母中的表达
  19. 未名企鹅极客 | 医药流向的数据仓库建模
  20. linux可以装在硬盘吗,linux系统可以从硬盘下安装吗?

热门文章

  1. 【转译】玩黑莓你必须了解的10件事
  2. 关于URL路径的基本使用
  3. mac 向mysql输入 数据_(mac系统下)mysql 入门
  4. python 100 days github_GitHub - Andyhe2019/Python-100-Days: Python - 100天从新手到大师
  5. 打开前端网页,使用npm run dev报错npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! gmall-admin@1.0.0 dev
  6. mysql 联合索引底层结构_MySQL联合索引底层数据结构
  7. python通用数据库连接_python-sqlalchemy 使用学习记录之基础连接数据库安装接篇...
  8. nodejs的moment操作时间
  9. Asp.net1.0和2.0网站共存只解决方法---使用应用程序池
  10. SQL Server 创建游标(cursor)