Lintcode:Longest Common Subsequence 解题报告

Longest Common Subsequence

Given two strings, find the longest comment subsequence (LCS).

Your code should return the length of LCS.

样例
For "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1

For "ABCD" and "EACB", the LCS is "AC", return 2

说明
What‘s the definition of Longest Common Subsequence?

* The longest common subsequence (LCS) problem is to find the longest subsequence common to all sequences in a set of sequences (often just two). (Note that a subsequence is different from a substring, for the terms of the former need not be consecutive terms of the original sequence.) It is a classic computer science problem, the basis of file comparison programs such as diff, and has applications in bioinformatics.

* https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

标签 Expand

SOLUTION 1:

DP.

1. D[i][j] 定义为s1, s2的前i,j个字符串的最长common subsequence.

2. D[i][j] 当char i == char j, D[i - 1][j - 1] + 1

当char i != char j, D[i ][j - 1], D[i - 1][j] 里取一个大的(因为最后一个不相同,所以有可能s1的最后一个字符会出现在s2的前部分里,反之亦然。

 1 public class Solution {
 2     /**
 3      * @param A, B: Two strings.
 4      * @return: The length of longest common subsequence of A and B.
 5      */
 6     public int longestCommonSubsequence(String A, String B) {
 7         // write your code here
 8         if (A == null || B == null) {
 9             return 0;
10         }
11
12         int lenA = A.length();
13         int lenB = B.length();
14         int[][] D = new int[lenA + 1][lenB + 1];
15
16         for (int i = 0; i <= lenA; i++) {
17             for (int j = 0; j <= lenB; j++) {
18                 if (i == 0 || j == 0) {
19                     D[i][j] = 0;
20                 } else {
21                     if (A.charAt(i - 1) == B.charAt(j - 1)) {
22                         D[i][j] = D[i - 1][j - 1] + 1;
23                     } else {
24                         D[i][j] = Math.max(D[i - 1][j], D[i][j - 1]);
25                     }
26                 }
27             }
28         }
29
30         return D[lenA][lenB];
31     }
32 }

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/dp/longestCommonSubsequence.java

时间: 2024-10-03 22:29:12

Lintcode:Longest Common Subsequence 解题报告的相关文章

LeetCode: Longest Common Prefix 解题报告

Longest Common Prefix Total Accepted: 24665 Total Submissions: 92370 My Submissions Question Solution Write a function to find the longest common prefix string amongst an array of strings. Show Tags Have you met this question in a real interview? Yes

Longest Common Prefix ——解题报告

    [题目] Write a function to find the longest common prefix string amongst an array of strings.     [分析] 公共前缀指的是所有字符串的前缀都相同.显然,这个最长公共前缀的长度不会超过所有字符串中最短的那个. 我们先求得最短串长minLen,然后遍历所有字符串中的前minLen是否相等.     [代码] 运行时间:7ms class Solution { public: string longe

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][

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a

Longest Common Subsequence Problem

The longest common subsequence (LCS) problem is the problem of finding the longest subsequence common to all sequences in a set of sequences (often just two sequences). It differs from problems of finding common substrings: unlike substrings, subsequ

【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

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

Dynamic Programming | Set 4 (Longest Common Subsequence)

首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", "bdf", "aeg", '"acefg"等都是"abcdefg"的子序列.因此,一个长度为n的序列拥有2^n中可能的子序列(序列中的每一个元素只有选或者不选两种可能,因此是2^n). Example: LCS for inp

[HackerRank] The Longest Common Subsequence

This is the classic LCS problem. Since it requires you to print one longest common subsequence, just use the O(m*n)-space version here. My accepted code is as follows. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5