Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 32693    Accepted Submission(s): 14786

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
 
Source
 
题意:求两个字符串的最长公共子序列的长度
 
题解: dp[i][j] 表示a串前i个字符与b串前j个字符 最长公共子序列的长度 N(n*m)
         当a[i]==a[j]时  dp[i][j]=dp[i-1][j-1]+1;
        否则 dp[i][j]=max(dp[i-1][j],dp[i][j-1])
 
 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char a[],b[];
int dp[][];
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 ;
}
 

HDU 1159 最长公共子序列(n*m)的更多相关文章

  1. HDU 1159 最长公共子序列

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

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

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

  3. HDU - 1503 最长公共子序列记录路径

    题意:先给两个水果的名字然后得出一个最短的序列包含这两个词. 思路:我一开始的思路是先求出最长公共子序列,然后做一些处理将其他的部分输出来:两种水果的字符串和最长公共子序列的字符串这三个字符串做对比, ...

  4. HDU 1159 Common Subsequence 公共子序列 DP 水题重温

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

  5. hdu 4681 最长公共子序列+枚举

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 #include<cstdio> #include<cstring> # ...

  6. hdu 1503 最长公共子序列

    /* 给两个串a,b.输出一个最短的串(含等于a的子序列且含等于b的子序列) */ #include <iostream> #include <cstdio> #include ...

  7. hdoj 1159最长公共子序列

     /*Common Subsequence A subsequence of a given sequence is the given sequence with some elements ( ...

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

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

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

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

随机推荐

  1. mysql 1055 的错误

    1.Err1055,出现这个问题往往是在执行sql语句时候,在最后一行会出现这个问题. [Err] 1055 - Expression #1 of ORDER BY clause is not in ...

  2. 【整理】PHP获取客户端真实IP地址详解

    php获取客户端IP地址有四种方法,这五种方法分别为REMOTE_ADDR.HTTP_CLIENT_IP.HTTP_X_FORWARDED_FOR.HTTP_VIA. REMOTE_ADDR 是你的客 ...

  3. html颜色实体符号表示汇总

    颜色的表示方法有许多种,列如black,#000000,rgb(0,0,0)都表示黑色.这三种表示方法分别为英文,十六进制,rgb格式.拥有下列颜色,足以使你的网页充满生机. 颜色名 十六进制颜色值 ...

  4. 关于PHPExcel 基础使用方法

    $dir=dirname(__FILE__);//找到当前脚本所在路径require_once $dir.'/PHPExcel/PHPExcel.php';$objPHPExcel=new PHPEx ...

  5. php获取设备的宽度和高度

    php获取设备的宽度和高度 如果前台没有传输当前请求的宽度和高度,我们有时候需要用php借助javascript获取屏幕的宽和高,以控制现实的样式. 代码如下: <?php /* * 获取设备宽 ...

  6. 2,版本控制git --分支

    有人把 Git 的分支模型称为它的`‘必杀技特性’',也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出. 为何 Git 的分支模型如此出众呢? Git 处理分支的方式可谓是难以置信的轻量, ...

  7. SpringMVC---四大注解

    SpringMVC四大注解 Component 通用标注,在不清楚使用哪个注解的时候,可以使用Component通用注解 Controller 标注web请求控制器 Service 标注Service ...

  8. MAC中mongodb的连接遇到的问题及调试

    今天在MAC环境下连接mongodb,遇到了一些报错,最终调试全部搞定.在此特做记录! 首先,mongod启动失败 上面有一句话是 exception in initAndListen: 20 Att ...

  9. sql中给逗号分隔的查询结果替换单引号

    技术交流群:233513714 第一种方法: SELECT * FROM pay_inf_config a WHERE a.id IN ( SELECT REPLACE ( concat('''', ...

  10. 直接选择排序&堆排序

    1.什么是直接选择排序? 直接选择排序(Straight Select Sort)是一种简单的排序方法,它的基本思想是:通过n-i次关键字之间的比较,从n-i+1个记录中选出关键字最小的记录,并和第i ...