-
Notifications
You must be signed in to change notification settings - Fork 88
/
LCS.java
79 lines (67 loc) · 2.07 KB
/
LCS.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
67
68
69
70
71
72
73
74
75
76
77
78
79
package cn.codepub.algorithms.strings;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
/**
* <p>
* Created with IntelliJ IDEA.
* </p>
* <p>
* ClassName:LCS
* </p>
* <p>
* Description:最长公共子序列
* </P>
*
* @author Wang Xu
* @version V1.0.0
*/
public class LCS {
public static String lcsBase(String inputX, String inputY) {
if (StringUtils.isEmpty(inputX) || StringUtils.isEmpty(inputY) || inputX.length() == 0 || inputY.length() == 0) {
return "";
} else {
char x = inputX.charAt(0);
char y = inputY.charAt(0);
if (x == y) {
return lcsBase(inputX.substring(1), inputY.substring(1)) + x;
} else {
return getMax(lcsBase(inputX.substring(1), inputY), lcsBase(inputX, inputY.substring(1)));
}
}
}
private static String getMax(String x, String y) {
Integer xLen = 0;
Integer yLen = 0;
if (StringUtils.isEmpty(x)) {
xLen = 0;
} else {
xLen = x.length();
}
if (StringUtils.isEmpty(y)) {
yLen = 0;
} else {
yLen = y.length();
}
if (xLen >= yLen) {
return x;
} else {
return y;
}
}
@Test
public void test() {
// 蓬安县房屋瓦片被震
String base1 = "落广西陕西震感强烈";
String base2 = "汶川地震致重庆4名学生死亡100多人受伤";
String s = lcsBase(base1, base2);
System.out.println(StringUtils.reverse(s));
String s2 = lcsBase("1233433236676", "98723765655423");
System.out.println(StringUtils.reverse(s2));
String s1 = lcsBase("123s212346我的大中国啊33z", "33z的大中国");
System.out.println(StringUtils.reverse(s1));
Integer a = 10000, b = 1000;
Integer c = 100, d = 100;
System.out.println(a == b);
System.out.println(c == d);
}
}