/*Common Subsequence

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.

Input

The program input is from the std input. Each data set in the input contains two


strings representing the given sequences. The sequences are separated by any number of


white spaces. The input data are correct.

Output

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*/

<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
#define maxn 1000
char str1[maxn], str2[maxn];
int dp[maxn][maxn];
int max(int a, int b)
{
return a > b ? a : b;
}
int gg()
{
int m= 0;
for(int i = 1; str1[i]; ++i){
for(int j = 1; str2[j]; ++j){
if(str1[i] == str2[j]){
dp[i][j] = dp[i-1][j-1] + 1;
}else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if(dp[i][j]>m) m= dp[i][j];
}
}
return m;
} int main()
{
while(scanf("%s%s", str1 + 1, str2 + 1)== 2){
printf("%d\n", gg());
}
return 0;
}
</span>

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

  1. HDU 1159 最长公共子序列(n*m)

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

  2. HDU 1159 最长公共子序列

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

  3. POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  4. HDU 1159 Common Subsequence:LCS(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...

  5. POJ 1159 Palindrome(最长公共子序列)

    http://poj.org/problem?id=1159 题意: 给出一个字符串,计算最少要插入多少个字符可以使得该串变为回文串. 思路: 计算出最长公共子序列,再用原长-LCS=所要添加的字符数 ...

  6. hdu 1159 Common Subsequence(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  7. hdu 1159 Common Subsequence(最长公共子序列 DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  8. HDU 1159 Common Subsequence 最长公共子序列

    HDU 1159 Common Subsequence 最长公共子序列 题意 给你两个字符串,求出这两个字符串的最长公共子序列,这里的子序列不一定是连续的,只要满足前后关系就可以. 解题思路 这个当然 ...

  9. HDU 1159 Common Subsequence --- DP入门之最长公共子序列

    题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[m ...

随机推荐

  1. web自动化测试:watir+minitest(四)

    脚本连跑: rake是ruby中的一个构建工具,和make很像.允许用ruby来写rakefile. 我们使用rake以任务的方式来运行我们的脚本集. 新建Rakefile文件,写入如下内容: req ...

  2. 详解Linux运维工程师应具备的十大技能

    Linux系统如果是学习可以选用Redhat或CentOS,特别是CentOS在企业中用得最多,当然还会有其它版本的,但学习者还是以这2个版本学习就行,因为这两个版本都是兄弟,没区别的,有空可以再研究 ...

  3. spark与storm比对与选型

    大数据实时处理平台市场上产品众多,本文着重讨论spark与storm的比对,最后结合适用场景进行选型. 一.spark与storm的比较 比较点 Storm Spark Streaming 实时计算模 ...

  4. MAC中如何配置两套android-sdk环境

    1 背景介绍 随着android studio的完善和普及,越来越多Android app开发者从Eclipse+ADT环境转到Android Studio(后文统一用AS表示).但是,AS往往需要较 ...

  5. .h 和 .cpp 切换快捷键设置(转)

    原文转自 https://blog.csdn.net/t163361/article/details/51859274 打开VS后依次选择如下 工具-> 选项-> 环境-> 键盘-& ...

  6. iOS_自定义毛玻璃效果

    http://www.2cto.com/kf/201408/329969.html 最终效果图: 关键代码: UIImage分类代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

  7. Linux单机安转Spark

    安装Spark需要先安装jdk及安装Scala. 1. 创建目录 > mkdir  /opt/spark > cd  /opt/spark 2. 解压缩.创建软连接 > tar  z ...

  8. Django简单设置cookies和session

    一.Cookie cookie及特点 Cookie是由服务器(网站)生成的,存储在浏览器端的 键值对数据(通常经过加密) 在响应请求时,服务器会把生成 Cookie数据 发给浏览器,浏览器会自动保存( ...

  9. 【C语言】32位,64位机器sizeof区别

    float, double : 采用IEEE标准浮点数格式,格式固定 float 32bit, double 64bit int一般和CPU寄存器长度有关,不过也和编译器,汇编器有关 由于C ,C++ ...

  10. 测试 System.SysUtils.TStringHelper

    来自:http://www.cnblogs.com/del/archive/2013/06/14/3135002.html -------------------------------------- ...