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. 

Input

abcfbc abfcab
programming contest
abcd mnp

Output

4
2
0

Sample Input

abcfbc abfcab
programming contest
abcd mnp

Sample Output

4
2
0
 #include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
#define N 1005
int dp[N+][N+];
char str1[N],str2[N];
int lcs(int len1,int len2)
{
int i,j;
int len=max(len1,len2);
for(i=;i<=len;i++){
dp[i][]=;
dp[][i]=;
}
for(i=;i<=len1;i++){
for(j=;j<=len2;j++){
if(str1[i-]==str2[j-]){
dp[i][j]=dp[i-][j-]+;
}
else {
dp[i][j]=max(dp[i][j-],dp[i-][j]);
}
}
}
return dp[len1][len2];
}
int main()
{
while(cin>>str1>>str2){
int len1=strlen(str1);
int len2=strlen(str2);
cout<<lcs(len1,len2)<<endl;
}
return ;
}

HDU - 1159 Common Subsequence (最长公共子序列)的更多相关文章

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

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

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

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

  3. C++版 - Lintcode 77-Longest Common Subsequence最长公共子序列(LCS) - 题解

    版权声明:本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - L ...

  4. POJ 1458 Common Subsequence(最长公共子序列LCS)

    POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列 ...

  5. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  6. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  7. LCS修改版(Longest Common Subsequence 最长公共子序列)

    题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉 ...

  8. POJ 1458 Common Subsequence 最长公共子序列

    题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include&l ...

  9. LCS(Longest Common Subsequence)最长公共子序列

    最长公共子序列(LCS)是一个在一个序列集合中(通常为两个序列)用来查找所有序列中最长子序列的问题.这与查找最长公共子串的问题不同的地方是:子序列不需要在原序列中占用连续的位置 .最长公共子序列问题是 ...

  10. PKU 1458 Common Subsequence(最长公共子序列,dp,简单)

    题目 同:ZJU 1733,HDU 1159 #include <stdio.h> #include <string.h> #include <algorithm> ...

随机推荐

  1. CentOS 6.4安装mongo的php扩展包

    最近安装mongo相关内容,因mongodb下载好解压即可使用,在这里我就不多说了,这里我分享下如何安装mongo的php扩展 首先下载扩展包https://github.com/mongodb/mo ...

  2. rabbitmq在kubernetes中持久化集群部署

    背景 Javashop电商系统的消息总线使用的事rabbitmq,在订单创建.静态页生成.索引生成等等业务中大量采用异步消息系统,这个对于mq高可用的要求有两个重要的考量: 1.集群化 2.可扩容 3 ...

  3. qt creator源码全方面分析(2-10-5)

    目录 The Plugin Manager, the Object Pool, and Registered Objects 插件管理器 对象池和已注册对象 The Plugin Manager, t ...

  4. 基于python的密码字典生成器

    最近在网上看到一些作品,然后对其进行了一些完善.只是用于学习,不要去干坏事哦.程序来源于网络,我只是做了一些优化. #!/usr/bin/python# -*- coding:utf-8 -*-# @ ...

  5. k8s调度器kube-scheduler

    kube-scheduler简介 调度是容器编排的重要环节,需要经过严格的监控和控制,现实生产通常对调度有各类限制,譬如某些服务必须在业务独享的机器上运行,或者从灾备的角度考虑尽量把服务调度到不同机器 ...

  6. kubeadm安装Kubernetes 1.14最佳实践

    前言 Kubernetes作为容器编排工具,简化容器管理,提升工作效率而颇受青睐.很多新手部署Kubernetes由于“上网”问题举步维艰,本文以实战经验详解kubeadm不用“翻墙”部署Kubern ...

  7. Basic Auth攻击

    1.Basic Auth认证简介 Basic身份认证,是HTTP 1.0中引入的认证方案之一.虽然方案比较古老,同时存在安全缺陷,但由于实现简单,至今仍有不少网站在使用它. 例如Apche Tomca ...

  8. 一条Sql的Spark之旅

    背景 ​ SQL作为一门标准的.通用的.简单的DSL,在大数据分析中有着越来越重要的地位;Spark在批处理引擎领域当前也是处于绝对的地位,而Spark2.0中的SparkSQL也支持ANSI-SQL ...

  9. 移动端 location.href 无法成功跳转页面

    最近做的移动端页面在请求成功后要跳转页面,通过location.href实现的跳转.但同事在测试时,安卓机可以成功跳转,苹果IOS确无法成功跳转.   解决办法:在链接后面加一个随机参数,这样就可以跳 ...

  10. 在centos7.x环境中SQL Server附加数据库

    第一步,准备好windows与Linux之间文件传递的工具,下载并安装 https://winscp.net/eng/download.php 第二步,把本地的数据库文件拷贝一份,放到别的文件夹中,因 ...