Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf1c3e93

Browse files
committed
05.02 (1) DP
1 parentcb028d7 commitf1c3e93

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Time : O() ; Space: O()
3+
* @tag : LintCode Copyright; Dynamic Programming
4+
* @by : Steven Cooks
5+
* @date: Sep 8, 2015
6+
***************************************************************************
7+
* Description:
8+
* Given two strings, find the longest common subsequence (LCS).
9+
* Your code should return the length of LCS.
10+
*
11+
* Example
12+
* For "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1.
13+
* For "ABCD" and "EACB", the LCS is "AC", return 2.
14+
*
15+
***************************************************************************
16+
* {@link http://www.lintcode.com/en/problem/longest-common-subsequence }
17+
*/
18+
package_02_LongestCommonSubsequence;
19+
20+
/** see test {@link _02_LongestCommonSubsequence.SolutionTest } */
21+
publicclassSolution {
22+
23+
publicintlongestCommonSubsequence(StringA,StringB) {
24+
int[][]dp =newint[A.length() +1][B.length() +1];
25+
for (inti =A.length();i >=0;i--) {
26+
for (intj =B.length();j >=0;j--) {
27+
intlen =0;
28+
if (i <A.length() &&j <B.length()) {
29+
if (A.charAt(i) ==B.charAt(j)) {
30+
len =1 +dp[i +1][j +1];
31+
}else {
32+
len =Math.max(dp[i +1][j],dp[i][j +1]);
33+
}
34+
}
35+
dp[i][j] =len;
36+
}
37+
}
38+
returndp[0][0];
39+
}
40+
41+
// verbose bottom-up dynamic programming version
42+
publicintlongestCommonSubsequence2(StringA,StringB) {
43+
int[][]dp =newint[A.length() +1][B.length() +1];
44+
for (inti =A.length();i >=0;i--) {
45+
for (intj =B.length();j >=0;j--) {
46+
intlen =0;
47+
if (i ==A.length() &&j ==B.length()) {
48+
len =0;
49+
}elseif (i ==A.length()) {
50+
len =0;
51+
}elseif (j ==B.length()) {
52+
len =0;
53+
}else {
54+
if (A.charAt(i) ==B.charAt(j)) {
55+
len =1 +dp[i +1][j +1];
56+
}else {
57+
len =Math.max(dp[i +1][j],dp[i][j +1]);
58+
}
59+
}
60+
dp[i][j] =len;
61+
}
62+
}
63+
returndp[0][0];
64+
}
65+
66+
// pure backtracking version
67+
publicintlongestCommonSubsequence3(StringA,StringB) {
68+
returnLCS(A,0,B,0);
69+
}
70+
71+
privateintLCS(StringA,inti,StringB,intj) {
72+
if (i ==A.length() &&j ==B.length()) {
73+
return0;
74+
}elseif (i ==A.length()) {
75+
return0;
76+
}elseif (j ==B.length()) {
77+
return0;
78+
}else {
79+
if (A.charAt(i) ==B.charAt(j)) {
80+
return1 +LCS(A,i +1,B,j +1);
81+
}else {
82+
returnMath.max(LCS(A,i +1,B,j),LCS(A,i,B,j +1));
83+
}
84+
}
85+
}
86+
87+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package_02_LongestCommonSubsequence;
2+
3+
importstaticorg.junit.Assert.*;
4+
5+
importorg.junit.After;
6+
importorg.junit.Before;
7+
importorg.junit.Rule;
8+
importorg.junit.Test;
9+
importorg.junit.rules.Timeout;
10+
11+
publicclassSolutionTest {
12+
13+
/** Test method for {@link _02_LongestCommonSubsequence.Solution } */
14+
Solutionsolution;
15+
16+
@Rule
17+
publicTimeoutglobalTimeout =newTimeout(200);
18+
19+
@Before
20+
publicvoidsetUp()throwsException {
21+
solution =newSolution();
22+
}
23+
24+
@After
25+
publicvoidtearDown()throwsException {
26+
solution =null;
27+
}
28+
29+
@Test
30+
publicvoidTest1() {
31+
StringA ="ABCD";
32+
StringB ="EDCA";
33+
intactual =solution.longestCommonSubsequence(A,B);
34+
intexpected =1;
35+
assertEquals(expected,actual);
36+
}
37+
38+
@Test
39+
publicvoidTest2() {
40+
StringA ="ABCD";
41+
StringB ="EACB";
42+
intactual =solution.longestCommonSubsequence(A,B);
43+
intexpected =2;
44+
assertEquals(expected,actual);
45+
}
46+
47+
@Test
48+
publicvoidTest3() {
49+
StringA ="XYZ";
50+
StringB ="EACB";
51+
intactual =solution.longestCommonSubsequence(A,B);
52+
intexpected =0;
53+
assertEquals(expected,actual);
54+
}
55+
56+
}

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Selected problems that are included in LintCode but are not included in Leetcode
1414
| 03.01|[Insert Node in Binary Search Tree](https://github.com/interviewcoder/lintcode/blob/master/03_binarytree%26divideconquer/_01_InsertNodeInABinarySearchTree/Solution.java)| BST`LintCode Copyright`||
1515
| 03.02|[Search Range In Binary Search Tree](https://github.com/interviewcoder/lintcode/blob/master/03_binarytree%26divideconquer/_02_SearchRangeInBinarySearchTree/Solution.java)| Binary Search Tree| in-order + pruning|
1616
| 04.01|[Longest Increasing Subsequence](https://github.com/interviewcoder/lintcode/blob/master/04_dynamicprogrammingI/_01_LongestIncreasingSubsequence/Solution.java)| Dynamic Programming||
17-
| 05.01|[Longest Common Substring](https://github.com/interviewcoder/lintcode/blob/master/05_dynamicprogrammingII/_01_LongestCommonSubstring/Solution.java)| Dynamic Programming||
17+
| 05.01|[Longest Common Substring](https://github.com/interviewcoder/lintcode/blob/master/05_dynamicprogrammingII/_01_LongestCommonSubstring/Solution.java)| Dynamic Programming||
18+
| 05.02|[Longest Common Subsequence](https://github.com/interviewcoder/lintcode/blob/master/05_dynamicprogrammingII/_02_LongestCommonSubsequence/Solution.java)| Dynamic Programming||

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp