Skip to content

Commit fcd197f

Browse files
committed
add algo
1 parent 40bdf6c commit fcd197f

File tree

19 files changed

+18989
-0
lines changed

19 files changed

+18989
-0
lines changed

exceptionLogs/cilin.log

Whitespace-only changes.

pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@
4646
<artifactId>guava</artifactId>
4747
<version>19.0-rc2</version>
4848
</dependency>
49+
<dependency>
50+
<groupId>org.projectlombok</groupId>
51+
<artifactId>lombok</artifactId>
52+
<version>1.16.6</version>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>org.hibernate</groupId>
57+
<artifactId>hibernate-core</artifactId>
58+
<version>5.0.2.Final</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.github.stuxuhai</groupId>
62+
<artifactId>jpinyin</artifactId>
63+
<version>1.0</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.apache.logging.log4j</groupId>
67+
<artifactId>log4j-core</artifactId>
68+
<version>2.4</version>
69+
</dependency>
4970
</dependencies>
5071

5172
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package cn.codepub.algorithms.commons;
2+
3+
import com.google.common.base.Preconditions;
4+
import com.google.common.collect.Sets;
5+
import org.apache.commons.lang3.StringUtils;
6+
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
/**
11+
* <p>
12+
* Created with IntelliJ IDEA. 2015/8/9 14:22
13+
* </p>
14+
* <p>
15+
* ClassName:JaccardSimilarityCoefficient
16+
* </p>
17+
* <p>
18+
* Description:杰卡德相似系数,是衡量两个集合的相似度的一种指标,J(A,B)=|A∩B|/|A∪B|
19+
* 杰卡德距离,是衡量两个集合中不同元素占所有元素的比例来衡量两个集合的区分度,Ja(A,B)=1-J(A,B)
20+
* </P>
21+
*
22+
* @author Wang Xu
23+
* @version V1.0.0
24+
* @since V1.0.0
25+
*/
26+
public class JaccardSimilarityCoefficient {
27+
public double getJSC(String str1, String str2) {
28+
if (StringUtils.isEmpty(str1) && StringUtils.isEmpty(str2)) {
29+
return 1;
30+
}
31+
Set<Character> s1 = new HashSet<Character>();
32+
Set<Character> s2 = new HashSet<Character>();
33+
str1 = Preconditions.checkNotNull(str1);
34+
str2 = Preconditions.checkNotNull(str2);
35+
char[] chars1 = str1.toCharArray();
36+
char[] chars2 = str2.toCharArray();
37+
for (char c : chars1) {
38+
s1.add(c);
39+
}
40+
for (char c : chars2) {
41+
s2.add(c);
42+
}
43+
44+
//求交集
45+
Set<Character> intersection = new HashSet<Character>();
46+
intersection.addAll(s1);
47+
intersection.retainAll(s2);
48+
49+
//求并集
50+
Set<Character> union = new HashSet<Character>();
51+
union.addAll(s1);
52+
union.addAll(s2);
53+
54+
return (double) intersection.size() / union.size();
55+
}
56+
57+
public static double calcBySets(String s0, String s1) {
58+
if (s0.isEmpty() && s1.isEmpty()) {
59+
return 1.0;
60+
}
61+
62+
Set<Character> words0 = new HashSet<Character>();
63+
Set<Character> words1 = new HashSet<Character>();
64+
for (int i = 0; i < s0.length(); i++) {
65+
words0.add(s0.charAt(i));
66+
}
67+
for (int i = 0; i < s1.length(); i++) {
68+
words1.add(s1.charAt(i));
69+
}
70+
71+
double intersect = Sets.intersection(words0, words1).size();
72+
double union = Sets.union(words0, words1).size();
73+
74+
System.out.println(Sets.union(words0, words1));
75+
return intersect / union;
76+
}
77+
78+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cn.codepub.algorithms.commons;
2+
import org.apache.commons.lang3.StringUtils;
3+
import org.junit.Test;
4+
5+
/**
6+
* <p>
7+
* Created with IntelliJ IDEA.
8+
* </p>
9+
* <p>
10+
* ClassName:LCS
11+
* </p>
12+
* <p/>
13+
* Description:最长公共子序列
14+
* </P>
15+
*
16+
* @author Wang Xu
17+
* @version V1.0.0
18+
*/
19+
public class LCS {
20+
public static String lcsBase(String inputX, String inputY) {
21+
if (StringUtils.isEmpty(inputX) || StringUtils.isEmpty(inputY) || inputX.length() == 0 || inputY.length() == 0) {
22+
return "";
23+
} else {
24+
char x = inputX.charAt(0);
25+
char y = inputY.charAt(0);
26+
if (x == y) {
27+
return lcsBase(inputX.substring(1), inputY.substring(1)) + x;
28+
} else {
29+
return getMax(lcsBase(inputX.substring(1), inputY), lcsBase(inputX, inputY.substring(1)));
30+
}
31+
32+
}
33+
}
34+
35+
private static String getMax(String x, String y) {
36+
Integer xLen = 0;
37+
Integer yLen = 0;
38+
if (StringUtils.isEmpty(x)) {
39+
xLen = 0;
40+
} else {
41+
xLen = x.length();
42+
}
43+
if (StringUtils.isEmpty(y)) {
44+
yLen = 0;
45+
} else {
46+
yLen = y.length();
47+
}
48+
49+
if (xLen >= yLen) {
50+
return x;
51+
} else {
52+
return y;
53+
}
54+
}
55+
56+
@Test
57+
public void test() {
58+
String s = lcsBase("我的大中国", "大中国我的");
59+
System.out.println(StringUtils.reverse(s));
60+
61+
String s2 = lcsBase("1233433236676", "98723765655423");
62+
63+
System.out.println(StringUtils.reverse(s2));
64+
65+
String s1 = lcsBase("123s212346我的大中国啊33z", "33z的大中国");
66+
System.out.println(StringUtils.reverse(s1));
67+
68+
}
69+
70+
71+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cn.codepub.algorithms.commons;
2+
import org.apache.commons.lang3.StringUtils;
3+
import org.junit.Test;
4+
5+
/**
6+
* <p>
7+
* Created with IntelliJ IDEA.
8+
* </p>
9+
* <p>
10+
* ClassName:LCS2
11+
* </p>
12+
* <p/>
13+
* Description:最长公共子串算法
14+
* </P>
15+
*
16+
* @author Wang Xu
17+
* @version V1.0.0
18+
*/
19+
public class LCS2 {
20+
private static Integer getComLen(String firstStr, String secondStr) {
21+
Integer comLen = 0;
22+
while (StringUtils.isNotEmpty(firstStr) && StringUtils.isNotEmpty(secondStr)) {
23+
if (firstStr.charAt(0) == secondStr.charAt(0)) {
24+
comLen += 1;
25+
firstStr = firstStr.substring(1);
26+
secondStr = secondStr.substring(1);
27+
} else {
28+
break;
29+
}
30+
}
31+
return comLen;
32+
}
33+
34+
private static String lcsBase(String inputX, String inputY) {
35+
Integer maxComLen = 0;
36+
Integer commonIndex = 0;
37+
38+
for (int i = 0; i < inputX.length(); i++) {
39+
for (int j = 0; j < inputY.length(); j++) {
40+
Integer comLen = getComLen(inputX.substring(i), inputY.substring(j));
41+
if (comLen > maxComLen) {
42+
maxComLen = comLen;
43+
commonIndex = i;
44+
}
45+
}
46+
}
47+
return inputX.substring(commonIndex, commonIndex + maxComLen);
48+
}
49+
50+
@Test
51+
public void test() {
52+
String s1 = "abcy交罚款撒了德萨 e";
53+
String s2 = "abcx交罚款dfdsafdsa范德萨 e";
54+
String s = lcsBase(s1, s2);
55+
System.out.println(s);
56+
}
57+
}
58+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cn.codepub.algorithms.commons;
2+
import org.apache.commons.lang3.StringUtils;
3+
import org.junit.Test;
4+
5+
/**
6+
* <p>
7+
* Created with IntelliJ IDEA.
8+
* </p>
9+
* <p>
10+
* ClassName:LCS3
11+
* </p>
12+
* <p/>
13+
* Description:使用动态规划解决最长公共子串
14+
* </P>
15+
*
16+
* @author Wang Xu
17+
* @version V1.0.0
18+
*/
19+
public class LCS3 {
20+
public static String lcsDp(String inputX, String inputY) {
21+
int xLen = 0;
22+
int yLen = 0;
23+
int maxLen = 0, maxIndex = 0;
24+
25+
if (StringUtils.isEmpty(inputX)) {
26+
xLen = 0;
27+
} else {
28+
xLen = inputX.length();
29+
}
30+
if (StringUtils.isEmpty(inputY)) {
31+
yLen = 0;
32+
} else {
33+
yLen = inputY.length();
34+
}
35+
36+
int dp[][] = new int[xLen][yLen];
37+
for (int i = 0; i < xLen; i++) {
38+
for (int j = 0; j < yLen; j++) {
39+
if (inputX.charAt(i) == inputY.charAt(j)) {
40+
if (i != 0 && j != 0) {
41+
dp[i][j] = dp[i - 1][j - 1] + 1;
42+
}
43+
if (0 == i || 0 == j) {
44+
dp[i][j] = 1;
45+
}
46+
if (dp[i][j] > maxLen) {
47+
maxLen = dp[i][j];
48+
maxIndex = i + 1 - maxLen;
49+
}
50+
}
51+
52+
}
53+
54+
}
55+
return inputX.substring(maxIndex, maxIndex + maxLen);
56+
}
57+
@Test
58+
public void test() {
59+
String s1 = "我是美abc国中defg国中间人";
60+
String s2 = "我是abdde我是美中国中国中国人";
61+
String s = lcsDp(s1, s2);
62+
System.out.println(s);
63+
}
64+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cn.codepub.algorithms.commons;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.junit.Test;
5+
6+
/**
7+
* <p>
8+
* Created with IntelliJ IDEA.
9+
* </p>
10+
* <p>
11+
* ClassName:LevenshteinDistance
12+
* </p>
13+
* <p>
14+
* Description:编辑距离算法
15+
* </P>
16+
*
17+
* @author Wang Xu
18+
* @version V1.0.0
19+
*/
20+
public class LevenshteinDistance {
21+
public static int leDistance(String inputX, String inputY) {
22+
int xLen = StringUtils.length(inputX) + 1;
23+
int yLen = StringUtils.length(inputY) + 1;
24+
int[][] dp = new int[xLen][yLen];
25+
for (int i = 0; i < xLen; i++) {
26+
dp[i][0] = i;
27+
}
28+
for (int j = 0; j < yLen; j++) {
29+
dp[0][j] = j;
30+
}
31+
for (int i = 1; i < xLen; i++) {
32+
for (int j = 1; j < yLen; j++) {
33+
if (inputX.charAt(i - 1) == inputY.charAt(j - 1)) {
34+
dp[i][j] = dp[i - 1][j - 1];
35+
} else {
36+
dp[i][j] = 1 + Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]);
37+
}
38+
39+
}
40+
}
41+
return dp[xLen - 1][yLen - 1];
42+
}
43+
44+
@Test
45+
public void test() {
46+
int leDistance = LevenshteinDistance.leDistance("", "aa");
47+
System.out.println(leDistance);
48+
}
49+
}

0 commit comments

Comments
 (0)