Longest Common Subsequence & Substring & prefix

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 "EACB", the LCS is "AC", return 2.

分析:

典型的DP。

 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 str1, String str2) {
 7         if (str1 == null || str2 == null) return 0;
 8         int[][] opt = new int[str2.length() + 1][str1.length() + 1];
 9
10         for (int j = 0; j <= str1.length(); j++) {
11             for (int i = 0; i <= str2.length(); i++) {
12                 if (i == 0 || j == 0) {
13                     opt[i][j] = 0;
14                 } else if (str2.charAt(i-1) == str1.charAt(j-1)) {
15                     opt[i][j] = opt[i-1][j-1] + 1;
16                 } else {
17                     opt[i][j] = Math.max(opt[i-1][j], opt[i][j-1]);
18                 }
19             }
20         }
21         return opt[str2.length()][str1.length()];
22     }
23 }

Longest Common Substring

Given two strings, find the longest common substring.

Return the length of it.

Notice

The characters in substring should occur continuously in original string. This is different with subsequence.

Example

Given A = "ABCD", B = "CBCE", return 2.

分析:

从头比到尾呗。

 1 public class Solution {
 2     /**
 3      * @param A, B: Two string.
 4      * @return: the length of the longest common substring.
 5      */
 6     public int longestCommonSubstring(String A, String B) {
 7         if (A == null || B == null || A.length() == 0 || B.length() == 0) return 0;
 8         int max = 0;
 9         for (int i = 0; i < B.length(); i++) {
10             for (int j = 0; j < A.length(); j++) {
11                 int incr = 0;
12                 while (i + incr < B.length() && j + incr < A.length() && (B.charAt(i + incr) == A.charAt(j + incr))) {
13                     incr++;
14                     max = Math.max(max, incr);
15                 }
16             }
17         }
18         return max;
19     }
20 }

Longest Common Prefix

Given k strings, find the longest common prefix (LCP).

Example

For strings "ABCD""ABEF" and "ACEF", the LCP is "A"

For strings "ABCDEFG""ABCEFG" and "ABCEFA", the LCP is "ABC"

分析:

取出第一个string和剩余的string相比即可。

 1 public class Solution {
 2     /**
 3      * @param strs: A list of strings
 4      * @return: The longest common prefix
 5      */
 6     public String longestCommonPrefix(String[] strs) {
 7         if (strs == null || strs.length == 0) return "";
 8         if (strs.length == 1) return strs[0];
 9
10         StringBuilder sb = new StringBuilder();
11
12         for (int j = 0; j < strs[0].length(); j++) {
13             for (int i = 1; i < strs.length; i++) {
14                 if (j >= strs[i].length() || strs[i].charAt(j) != strs[0].charAt(j)) {
15                     return sb.toString();
16                 }
17             }
18             sb.append(strs[0].charAt(j));
19         }
20         return sb.toString();
21     }
22 }
时间: 2024-10-13 15:12:14

Longest Common Subsequence & Substring & prefix的相关文章

[Algorithms] Longest Common Subsequence

The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the length of the longest sequence r, which is a subsequence of both s and t. Do you know the difference between substring and subequence? Well, substring i

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 "

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

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

[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

HDU 2253 Longest Common Subsequence Again

其实这个题我还不会,学长给了一个代码交上去过了,据说用到了一种叫做位压缩的技术,先贴代码吧,以后看懂了再来写 #include <stdio.h> #include <string.h> #define M 30005 #define SIZE 128 #define WORDMAX 3200 #define BIT 32 char s1[M], s2[M]; int nword; unsigned int str[SIZE][WORDMAX]; unsigned int tmp1

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

UVA 10405 Longest Common Subsequence

最长公共子系列,简单的dp,不过注意有空格,所以,在读字符串的时候,尽量用gets读,这样基本没问题 #include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; int dp[1001][1001]; int MAX(int x,int y) { if (x>y) return x; else return y; } int ma