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])
 
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. using namespace std;
  5. char a[],b[];
  6. int dp[][];
  7. int main()
  8. {
  9. while(cin>>a>>b)
  10. {
  11. int len1=strlen(a);
  12. int len2=strlen(b);
  13. memset(dp,,sizeof(dp));
  14. for(int i=;i<=len1;i++)
  15. for(int j=;j<=len2;j++)
  16. {
  17. if(a[i-]==b[j-])
  18. dp[i][j]=dp[i-][j-]+;
  19. else
  20. dp[i][j]=max(dp[i-][j],dp[i][j-]);
  21. }
  22. cout<<dp[len1][len2]<<endl;
  23. }
  24. return ;
  25. }
 

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. spring-AspectJ

    动态代理 ProxyFactoryBean织入切面数量太多不利于围护 BeanNameAutoProxyCreater-------------根据Bean名称创建代理 DefaultAdvisorA ...

  2. eclipse引入jquery文件报错解决

    以下内容是小编给大家带来的关于Eclipse引入jquery报错如何解决的全部叙述,具体内容如下所示: 第一步: 去除eclipse的JS验证: 将windows->preference-> ...

  3. tcl之控制流-for

  4. MySQL基础 (麦子学员 php 第二阶段)

    通过my.ini配置文件修改字符集:客户端字符集设置:[mysql]default-character-set=utf8 [mysqld] character-set-server=utf8 .设置之 ...

  5. 1014-34-首页15-计算原创微博的frame------计算cell的高度---计算 UILabel 的 CGSize 的方法

    一.总体思路: 在控制器中,每次拿到数据模型(请求了数据.加载新微博)的时候,就调用 - (NSArray *)stausFramesWithStatuses:(NSArray *)statuses, ...

  6. Leetcode 538. 把二叉搜索树转换为累加树

    题目链接 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ 题目描述 大于它的节点值之和. 例如: 输入: ...

  7. 笔记-DB-mongodb-常用操作-1

    笔记-DB-mongodb-常用操作-1 1.  启动及连接 1.1.  启动 启动mongod windows下: 1.   如已添加服务 net start <service name> ...

  8. 实时查询引擎 - Facebook Presto 介绍与应用

    1. Presto 是什么   Facebook presto是什么,继Facebook创建了HIVE神器后的又一以SQL语言作为接口的分布式实时查询引擎,可以对PB级的数据进行快速的交互式查询.它支 ...

  9. PHP代码审计2-常用超全局变量,常用命令注入,常用XSS漏洞审计,文件包含

    超全局变量 $GLOBALS — 引用全局作用域中可用的全部变量$_SERVER — 服务器和执行环境信息$_GET — HTTP GET 变量$_POST — HTTP POST 变量$_FILES ...

  10. 8 Django 模型层(1)--orm简介

    ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...