-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathLCS2.java
59 lines (53 loc) · 1.6 KB
/
LCS2.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
package cn.codepub.algorithms.strings;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
/**
* <p>
* Created with IntelliJ IDEA.
* </p>
* <p>
* ClassName:LCS2
* </p>
* <p>
* Description:最长公共子串算法
* </P>
*
* @author Wang Xu
* @version V1.0.0
*/
public class LCS2 {
private static Integer getComLen(String firstStr, String secondStr) {
Integer comLen = 0;
while (StringUtils.isNotEmpty(firstStr) && StringUtils.isNotEmpty(secondStr)) {
if (firstStr.charAt(0) == secondStr.charAt(0)) {
comLen += 1;
firstStr = firstStr.substring(1);
secondStr = secondStr.substring(1);
} else {
break;
}
}
return comLen;
}
private static String lcsBase(String inputX, String inputY) {
Integer maxComLen = 0;
Integer commonIndex = 0;
for (int i = 0; i < inputX.length(); i++) {
for (int j = 0; j < inputY.length(); j++) {
Integer comLen = getComLen(inputX.substring(i), inputY.substring(j));
if (comLen > maxComLen) {
maxComLen = comLen;
commonIndex = i;
}
}
}
return inputX.substring(commonIndex, commonIndex + maxComLen);
}
@Test
public void test() {
String s1 = "abcy交罚款撒了德萨 e";
String s2 = "abcx交罚款dfdsafdsa范德萨 e";
String s = lcsBase(s1, s2);
System.out.println(s);
}
}