/*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. 写把proto函数搞清楚

    在做blk层之前,先把proto搞清楚 ffi_lua metatype可以给函数加方法, lua中冒号是啥意思?冒号会传入self,但是点号不会传入self

  2. swagger-ui enum select

    swagger-ui enum select Array[string] https://dejanstojanovic.net/aspnet/2018/march/displaying-multip ...

  3. transform perspective的层级问题

    如上图,在积分的数字元素上,使用了transform perspective,其层级就穿透了上面的遮罩层,关键代码如下: .mask { position: fixed; z-index:; } .f ...

  4. BZOJ[Sdoi2014]数表 莫比乌斯反演

    [Sdoi2014]数表 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2383  Solved: 1229[Submit][Status][Disc ...

  5. lesson 5

    C#中的委托(delegate)与事件(event) 一.委托就是中间人的意思,c#中的委托允许将一个类中的方法传递给另一个能调用该方法的类的某个对象.程序员可以将A类的一个方法m(被包含在某个del ...

  6. JAVA下几个问题

    一.Server MyEclipse Tomcat v8.5 was unable to start within 45 seconds. If the server requires more ti ...

  7. Linux进程管理与调度-之-目录导航【转】

    转自:http://blog.csdn.net/gatieme/article/details/51456569 版权声明:本文为博主原创文章 && 转载请著名出处 @ http:// ...

  8. linux内核分析之进程地址空间【转】

    转自:http://blog.csdn.net/bullbat/article/details/7106094 版权声明:本文为博主原创文章,未经博主允许不得转载. 本文主要介绍linux内核中进程地 ...

  9. 华为上机测试题(表达式运算-java)

    PS:自己写的,自测试OK,供大家参考. 补充:数据解析的过程,评论区有更好的处理方式,可参考. /* * 输入一个表达式,3*8+7-2,没有括号 输出结果 */ /* 本程序暂不考虑容错处理 */ ...

  10. java基础练习 16

    public class Sixtheen { /*利用递归方法求5!.*/ public static void main(String[] args){ System.out.println(&q ...