AOJ_ALDS1_10_C Longest Common Subsequence【LCS+DP】

Longest Common Subsequence
Aizu - ALDS1_10_C

For given two sequences X and Y, a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y. For example, if X={a,b,c,b,d,a,b} and Y={b,d,c,a,b,a}, the sequence {b,c,a} is a common subsequence of both X and Y. On the other hand, the sequence {b,c,a} is not a longest common subsequence (LCS) of X and Y, since it has length 3 and the sequence {b,c,b,a}, which is also common to both X and Y, has length 4. The sequence {b,c,b,a} is an LCS of X and Y, since there is no common subsequence of length 5 or greater.

Write a program which finds the length of LCS of given two sequences X and Y. The sequence consists of alphabetical characters.

Input

The input consists of multiple datasets. In the first line, an integer qq which is the number of datasets is given. In the following 2×q lines, each dataset which consists of the two sequences X and Y are given.

Output

For each dataset, print the length of LCS of X and Y in a line.

Constraints

1≤q≤150
1≤ length of X and Y ≤1,000
q≤20 if the dataset includes a sequence whose length is more than 100

Sample Input 1

3
abcbdab
bdcaba
abc
abc
abc
bc

Sample Output 1

4
3
2

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

问题链接AOJ_ALDS1_10_C Longest Common Subsequence
问题简述:(略)
问题分析
????动态规划问题,是一个标准模板题,套模板就可以了。
????需要注意字符串长度!
程序说明:(略)
????该程序使用C++语言编写,使用了string变量,并且把LCS算法封装到函数lcs()中。
题记:(略)
参考链接POJ1458 HDU1159 ZOJ1733 UVALive2759 Common Subsequence【最长公共子序列+DP】

AC的C++语言程序如下:

/* AOJ_ALDS1_10_C Longest Common Subsequence */

#include <iostream>
#include <string.h>

using namespace std;

const int N = 1000;

int lcs(string& a, string& b)
{
    int dp[N + 1][N + 1];
    memset(dp, 0, sizeof(dp));
    for(int i = 0; i < (int)a.size(); i++)
        for(int j = 0; j < (int)b.size(); j++)
            if(a[i] == b[j])
                dp[i + 1][j + 1] = dp[i][j] + 1;
            else
                dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);

    return dp[a.size()][b.size()];
}

int main()
{
    string s1, s2;
    int q;

    cin >> q;
    for(int i = 1; i <= q; i++) {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }

    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10048064.html

时间: 2024-08-06 02:06:55

AOJ_ALDS1_10_C Longest Common Subsequence【LCS+DP】的相关文章

UVA10405 Longest Common Subsequence【LCS+DP】

Given two sequences of characters, print the length of the longest common subsequence of both sequences. ????Sequence 1: ????Sequence 2: ????For example, the longest common subsequence of the following two sequences 'abcdgh' ans 'aedfhr' is 'adh' of

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

问题描述:序列X={x1,x2,-,xn},Y={y1,y2,-,yn},当Z={z1,z2-,zn}是X的严格递增下标顺序(可以不连续)的子集,也是Y的严格递增下标顺序(可以不连续)的子集,则Z是X和Y的公共子序列.例如X={A,B,C,B,D,A,B},Y={B,D,C,A,B,A},{B,C,A}.{B,C,B,A}.{B,D,A,B}都是X和Y的公共子序列.其中最长的公共子序列叫做Longest common subsequence,即经典的LCS. 具体点:char[]xArray和c

hdoj 1159 Common Subsequence【LCS】【DP】

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28494    Accepted Submission(s): 12735 Problem Description A subsequence of a given sequence is the given sequence with some e

POJ1458 &amp;&amp; HDOJ1159 Common Subsequence【LCS】

题目链接(POJ) :http://poj.org/problem?id=1458 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40156   Accepted: 16162 Description A subsequence of a given sequence

leetcode:Longest Common Prefix【Python版】

1.当strs为空,直接输出“” 2.当strs中含有“”,直接输出“” 3.strs[0]的最长长度由最短公共长度l决定(code line:15) 1 class Solution: 2 # @return a string 3 def longestCommonPrefix(self, strs): 4 if strs == []: 5 return "" 6 for i in range(1,len(strs)): 7 l1 = len(strs[0]) 8 l2 = len(

【leetcode】1143. Longest Common Subsequence

题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order

Longest Common Subsequence (DP)

Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Example For "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1. For "ABCD" and &quo

LCS (Longest Common Subsequence)

LCS是两个序列相似性的一种度量方法: 若序列s1:2,5,7,9,3,1,2 s2:3,5,3,2,8 则LCS为:5,3,2 思路可参考:http://www.csie.ntnu.edu.tw/~u91029/LongestCommonSubsequence.html 具体代码实现为: 1 #include<iostream> 2 using namespace std; 3 4 int s1[7+1]={0,2,5,7,9,3,1,2}; 5 int s2[7+1]={0,3,5,3,2

uva10405 - Longest Common Subsequence(LIS,最长共同自序列)

题目:uva10405 - Longest Common Subsequence(LIS,最长共同自序列) 题目大意:找出两个字符串中的最长公共的子序列. 解题思路:这类问题是第一次接触,不知道怎么做.百度了一下,发现了递推公式:dp[i][j]:代表第一个字符串的前i个字符和第二个字符串的前j个字符比较能得到的最长的公共子序列.s[i] == s[j] ,dp[i][j] = dp[i - 1][j - 1] + 1: s[i] != s[j] , dp[i][j] = Max (dp[i][