Common Subsequence

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

http://blog.csdn.net/a_eagle/article/details/7213236

dp思想,不断更新当前的最长值。

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std; int dp[][];
char a[],b[]; int main()
{
while(cin>>a>>b)
{
int len1=strlen(a);
int len2=strlen(b);
memset(dp,,sizeof(dp));
for(int i=; i<=len1; i++)
for(int j=; j<=len2; j++)
if(a[i-]==b[j-])
dp[i][j]=dp[i-][j-]+;
else
dp[i][j]=max(dp[i-][j],dp[i][j-]);
cout<<dp[len1][len2]<<endl;
}
return ;
}

hdu1159 最长公共子序列的更多相关文章

  1. hdu1503 最长公共子序列变形

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1503 题意:给出两个字符串 要求输出包含两个字符串的所有字母的最短序列.注意输出的顺序不能 ...

  2. 动态规划---最长公共子序列 hdu1159

    hdu1159 题目要求两个字符串最长公共子序列, 状态转换方程   f[i][j]=f[i-1][j-1]+1; a[i]=b[j]时 f[i][j]=MAX{f[i-1][j],f[i][j-1] ...

  3. LCS最长公共子序列HDU1159

    最近一直在学习算法,基本上都是在学习动态规划以及字符串.当然,两者交集最经典之一则是LCS问题. 首先LCS的问题基本上就是在字符串a,b之间找到最长的公共子序列,比如 YAOLONGBLOG 和 Y ...

  4. ACMDP之最长公共子序列长度—HDU1159

    Common Subsequence Problem Description A subsequence of a given sequence is the given sequence with ...

  5. 【水:最长公共子序列】【HDU1159】【Common Subsequence】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. hdu1159 dp(最长公共子序列)

    题意:给两个字符串,求这两个字符串的最长公共子序列的长度 因为之前集训的时候做过,所以现在即使会做也并不是什么稀奇的事,依旧为了自己的浅薄感到羞愧啊``` 解法就是通过两个字符串的每个字符互相比较,根 ...

  7. 最长公共子序列-Hdu1159

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

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

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

随机推荐

  1. 如何获得images.xcassets 中图片的路径?

    UIImage加载图片的方式以及Images.xcassets对于加载方法的影响 重点: Images.xcassets中的图片资源只能通过imageNamed:方法加载,通过NSBundle的pat ...

  2. [Android Pro] static 和 Volatile 的区别

    reference to : http://blog.sina.com.cn/s/blog_4e1e357d0101i486.html static也是各个业务方可以去全局修改: volatile是处 ...

  3. September 12th 2016 Week 38th Monday

    After all, tomorrow is another day. 不管怎样,明天又是全新的一天. Another day? Maybe.  Remember when you reach for ...

  4. app性能测试点、安全测试点总结

  5. html 中几次方,平方米,立方米,下标,上标,删除线等的表示方法

    html 中几次方,平方米,立方米,上标,下标,删除线等的表示方法 上标下标删除线 小号字  M2 54 X24+Y1<3=100 NN <sup>上标</sup> &l ...

  6. ASP.NET Web API 上传文件

    HTML表单: <form id="form1" method="post" enctype="multipart/form-data" ...

  7. HTML CSS微信CSS显示一些总结

    微信显示网页是调用腾讯自带的浏览器内核,由于腾讯浏览器内核对css展示效果没有谷歌浏览器好,导致用谷歌浏览器写好的网页,放到微信页面之后,显示的效果就发生变化,所以调整css样式显得那么吃力: 1. ...

  8. POJ2065 SETI(高斯消元 同模方程)

    (a1 * 1^0  +   a2 * 1^1  + ...  an * 1^n - 1) % P = f1 .... (a1 * n^0  +   a2 * n^1  + ...  an - 1 * ...

  9. POJ2406 Power Strings(KMP,后缀数组)

    这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...

  10. PowerDesigner连接Oracle数据库生成数据模型【数据源连接方式】

    1.进入操作系统的管理工具 2.选择ODBC数据源[32位或64位] 3.列表中是当前数据库已有的数据源,右侧点击添加按钮,添加适合自己的数据源 4.在列表中选择索要连接数据库的ODBC驱动[这里我要 ...