Common Subsequence(最长公共子序列)

原创 2018年04月15日 14:27:47
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 58428 Accepted: 24399

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.

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

Source


就是求最长公共子序列


代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#define ll lonng long
using namespace std;
const int maxn = 207;

string a, b;
int dp[maxn][maxn];


int main()
{
    while(cin>>a>>b) {
        int lena = a.length(), lenb = b.length();
        memset(dp, 0, sizeof(dp));
        for(int i = 0; i < lena; i++) {
            for(int j = 0; j < lenb; j++) {
                if(a[i] == b[j]) dp[i+1][j+1] = dp[i][j] + 1;
                else {
                    dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
                }
            }
        }
        printf("%d\n", dp[lena][lenb]);
    }
    return 0;
}

版权声明: https://blog.csdn.net/weixin_39792252/article/details/79947738

python实现Longest Common Subsequence最长公共子序列算法

最长公共子序列是很基本的算法,只是最近用到了就又拿来学习一下,网上有很多很多的Java版本的,的确写的也很不错,思想都很好,大致上分为三种: 1.基于递归的思想 2.基于动态规划的思想 3.基于Has...
  • Together_CZ
  • Together_CZ
  • 2017-07-06 11:20:20
  • 1039

【LeetCode】Longest Common Subsequence最长公共子序列(求出某一解+LCS长度) - Medium

Longest Common Substring Given two strings, find the longest common substring.Return the length of i...
  • ljyljyok
  • ljyljyok
  • 2017-09-09 09:12:00
  • 1640

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

POJ 1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串...
  • u013480600
  • u013480600
  • 2014-11-03 15:14:57
  • 2928

lintcode longest-common-subsequence 最长公共子序列 证明

问题描述lintcode参考coursera-北京大学-算法基础-动态规划(1)-几个例题笔记动态规划问题,同样要建表。 buff[i][j]表示以字符串A的第i位结束,以字符串B的第j位结束,的最...
  • waltonhuang
  • waltonhuang
  • 2016-07-26 09:12:33
  • 647

最长公共子序列(longest common subsequence) 动态规划

问题题目:[Common Subsequence]思路代码#include #include #include #include int cal_lcs( const std::string&...
  • Kang_TJU
  • Kang_TJU
  • 2016-11-29 22:31:15
  • 250

[Lintcode]Longest Common Subsequence 最长公共子序列

Given two strings, find the longest common subsequence (LCS). Your code should return the length ...
  • jc69186918
  • jc69186918
  • 2016-10-18 18:31:47
  • 402

杭电-1159 Common Subsequence(最长公共子序列)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot...
  • lu_1110
  • lu_1110
  • 2016-08-12 11:36:23
  • 172

Common Subsequence(最长公共子序列+动态规划)hdu1159 经典

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe...
  • u010579068
  • u010579068
  • 2015-10-17 21:06:04
  • 1783

【算法导论学习-29】动态规划经典问题02:最长公共子序列问题(Longest common subsequence,LCS)

《算法导论中》动态规划经典问题——最长公共子序列问题(Longest common subsequence,LCS)的java实现...
  • BrilliantEagle
  • BrilliantEagle
  • 2014-09-03 21:42:19
  • 1543

最长公共子序列Longest Common Subsequence - Super Jiju的小窝_ To be with my Dearest Jessie

  • 2008年04月26日 13:36
  • 5KB
  • 下载
收藏助手
不良信息举报
您举报文章:Common Subsequence(最长公共子序列)
举报原因:
原因补充:

(最多只允许输入30个字)