题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159

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
程序原理如下状态方程:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;int smax(int x,int y){return x >=y ? x : y;
}int maxstr[20001][20001];
char str1[2000],str2[2000];int main(){int len1,len2;while(scanf("%s%s",&str1, &str2)!=EOF){len1=strlen(str1);len2=strlen(str2);for(int i=0; i<=len1; ++i){maxstr[i][0]=0;//初始化边界,过滤掉0的情况}for(int i=0; i<=len2; ++i){maxstr[0][i]=0;}//填充矩阵for(int i=1; i<=len1; ++i){for(int j=1; j<=len2; ++j){if(str1[i-1]==str2[j-1])//相等的情况maxstr[i][j]=maxstr[i-1][j-1]+1;else if(maxstr[i-1][j] >= maxstr[i][j-1]){//比较“左边”和“上边“,根据其max来填充//maxstr[i][j]=smax(maxstr[i-1][j],maxstr[i][j-1]);maxstr[i][j]=maxstr[i-1][j];}else{maxstr[i][j]=maxstr[i][j-1];}}}printf("%d\n",maxstr[len1][len2]);}return 0;
} 

资料链接:http://www.cnblogs.com/huangxincheng/archive/2012/11/11/2764625.html

HD 1159 Common Subsequence (最长公共子序列)相关推荐

  1. C++longest common subsequence最长公共子序列的实现(附完整源码)

    C++longest common subsequence最长公共子序列 longest common subsequence最长公共子序列的完整源码(定义,实现,main函数测试) longest ...

  2. leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)

    一.题目大意 标签: 动态规划 https://leetcode.cn/problems/longest-common-subsequence 给定两个字符串 text1 和 text2,返回这两个字 ...

  3. LCS最长公共子序列和LIS最长上升子序列——例题剖析

    一.LCS最长公共子序列 最长公共子序列(LCS)问题算法详解+例题(转换成LIS,优化为O(nlogn),看不懂你来打我) longest comment subsequence 模板题 longe ...

  4. 动态规划—最长公共子序列问题 HDU-1159 Common Subsequence

    动态规划-最长公共子序列问题 Common Subsequence [ HDU - 1159 ] A subsequence of a given sequence is the given sequ ...

  5. 最长公共子序列(LCS)问题 Longest Common Subsequence 与最长公告字串 longest common substr...

    问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X="x0,x1,-,xm-1",序列Y=& ...

  6. 【算法导论学习-29】动态规划经典问题02:最长公共子序列问题(Longest common subsequence,LCS)...

    2019独角兽企业重金招聘Python工程师标准>>> 问题描述:序列X={x1,x2,-,xn},Y={y1,y2,-,yn},当Z={z1,z2-,zn}是X的严格递增下标顺序( ...

  7. LeetCode——1143. 最长公共子序列(Longest Common Subsequence)[中等]——分析及代码(Java)

    LeetCode--1143. 最长公共子序列[Longest Common Subsequence][中等]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 (1)思路 (2)代 ...

  8. POJ 1458 Common Subsequence DP LCS 最长公共子序列

    最长公共子序列,照抄<算法设计与分析导论>P138-140 设输入的两个字符串分别为a1,a2,```,am(串a) b1,b2,````,bn(串b) 设d(i,j)为字符串a1,a2, ...

  9. hdu 1159(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 思路:dp[i][j]表示s1从0~i-1,s2从0~j-1的最长公共子序列: 递推方程为:dp ...

最新文章

  1. 计算机视觉 | 哥大读博五年总结
  2. 关于Cocos2d-x中精灵节点的透明度的设置
  3. app.vue添加子组件
  4. C指针原理(39)-GLIB
  5. Unable to open socket file: target process not responding or HotSpot VM not loaded The -F option can
  6. linux父子进程同步实验,Linux-父子进程的简单同步
  7. 【刷出存在感】锋会圆桌现场
  8. linux c之通过管道父子进程实现同步通信
  9. 控制反转_Spring:IOC 控制反转
  10. ActiveRecord模式整理
  11. 深入 JavaScript 装饰器
  12. Python入门很难吗? 为什么越来越多的人都学Python?
  13. python怎么一步步调试_PyCharm入门第一步(二)——调试第一个Python应用程序
  14. 如何批量裁剪pdf文件中的白边
  15. php读取移动硬盘数据,什么是移动硬盘
  16. java修改文件只读_java修改文件为只读权限
  17. matlab 高级绘图函数
  18. OpenCV实战——文本检测
  19. 《Java8实战》第9章 重构、测试和调试
  20. element-ui upload上传技巧

热门文章

  1. 印象笔记无法连服务器(internet explore的问题)
  2. 蓝色巨人的硬件业务:存储营收或将下滑
  3. 使用HBase Client访问阿里云NoSQL数据库表格存储
  4. 【java】map的几种遍历方式
  5. IT十八掌作业_java基础第二天_进制转换原理和补码存储方式
  6. WebStorm荣获InfoWorld2014年度科技奖
  7. 安装完成Fedora 15 LXDE 定制版后的操作
  8. FTP基础知识及排障方法
  9. js如何往div里写div
  10. phd or domain