/*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. Android初学者必知会的编程规范

    在安卓学习中,我们首先需要掌握的就是Android编程的一些规范,只有掌握了这些规范,后面的深入学习才能开展.今天小编在一个Android培训网站上搜罗了一些,Android初学者不得不知的开发规范, ...

  2. Codeforces Round #389 (Div. 2) 752F(树的权值重心)

    题目大意 给定2k个队伍分别住在2k个城市里,需要设定若干个城市,然后选取2个队伍要在它们的最短路径上设一个城市作为休息站 要求设立最少的休息站,然后输出如何安排2个队伍 首先若干个其实就是在坑人,实 ...

  3. Tomcat学习笔记(三)

    Tomcat连接器 tomcat连接器是tomcat的一个核心组件,在tomcat4中的实现原理如下 1.实现Connector接口 2.创建Request对象 3.创建Response对象 tomc ...

  4. Educational Codeforces Round 42 (Rated for Div. 2) A

    A. Equator time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  5. python使用openpyxl操作excel

    def initExcel(): file_path = "test.xlsx" file = load_workbook(file_path) table = file[&quo ...

  6. python面试经典315

    期待的是可以检验自己学习的成功:苦逼的是怎么又有东西没记住,但我们依然每天坚持一遍.一遍又一遍指导记住为止. 第一部分 Python基础篇(80题) 为什么学习Python? 通过什么途径学习的Pyt ...

  7. wp检测是否是手机网络还是wifi网络

    原文发布时间为:2013-06-22 -- 来源于本人的百度文章 [由搬家工具导入] ),newNameResolutionCallback(handle =>{NetworkInterface ...

  8. Linux Mint---fcitx中文,日语输入法

    安装中文输入法,我这边选了小企鹅,先前用过scim,稍微比较了下,效果还是这个好 安装可一步即可搞定 apt-get install fcitx fcitx-table-wubi-large fcit ...

  9. zero(NOIP模拟赛 Round 4)

    题目描述 假设x=N!,那么x的末尾有多少个零呢? 输入 一行,一个整数N. 输出 输出只有一个整数,表示x末尾零的个数. 这道题目,我们看一看数据范围, 10^1000肯定是高精啦! 然后我们再想一 ...

  10. sklearn的常用函数以及参数

    sklearn可实现的函数或者功能可分为如下几个方面 1.分类算法2.回归算法3.聚类算法4.降维算法5.模型优化6.文本预处理 其中分类算法和回归算法又叫监督学习,聚类算法和降维算法又叫非监督学习 ...