forked from shijiebei2009/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCS3.java
66 lines (59 loc) · 1.68 KB
/
LCS3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.codepub.algorithms.strings;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
/**
* <p>
* Created with IntelliJ IDEA.
* </p>
* <p>
* ClassName:LCS3
* </p>
* <p>
* Description:使用动态规划解决最长公共子串
* </P>
*
* @author Wang Xu
* @version V1.0.0
*/
public class LCS3 {
public static String lcsDp(String inputX, String inputY) {
int xLen = 0;
int yLen = 0;
int maxLen = 0, maxIndex = 0;
if (StringUtils.isEmpty(inputX)) {
xLen = 0;
} else {
xLen = inputX.length();
}
if (StringUtils.isEmpty(inputY)) {
yLen = 0;
} else {
yLen = inputY.length();
}
int dp[][] = new int[xLen][yLen];
for (int i = 0; i < xLen; i++) {
for (int j = 0; j < yLen; j++) {
if (inputX.charAt(i) == inputY.charAt(j)) {
if (i != 0 && j != 0) {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
if (0 == i || 0 == j) {
dp[i][j] = 1;
}
if (dp[i][j] > maxLen) {
maxLen = dp[i][j];
maxIndex = i + 1 - maxLen;
}
}
}
}
return inputX.substring(maxIndex, maxIndex + maxLen);
}
@Test
public void test() {
String s1 = "我是美abc国中defg国中间人";
String s2 = "我是abdde我是美中国中国中国人";
String s = lcsDp(s1, s2);
System.out.println(s);
}
}