-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubstring.java
More file actions
32 lines (27 loc) · 931 Bytes
/
LongestCommonSubstring.java
File metadata and controls
32 lines (27 loc) · 931 Bytes
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
/**
* 最长公共子串
*/
public class LongestCommonSubstring{
public static void main(String[] args) {
String str1 = "abcdefg";
String str2 = "abcagabcdef";
int tab[][] = new int[str1.length()][str2.length()];
int maxValue = 0;
// 子串2在子串1中查找
for (int i = 0; i < str1.length(); i++) {
for (int j = 0; j < str2.length(); j++) {
if (i == 0) {
tab[i][j] = str2.charAt(j) == str1.charAt(i) ? 1 : 0;
}else if (j == 0) {
tab[i][j] = str2.charAt(j) == str1.charAt(i) ? 1 : 0;
}else if (str2.charAt(j) == str1.charAt(i)){
tab[i][j] = tab[i-1][j-1] + 1;
}else{
tab[i][j] = 0;
}
maxValue = Math.max(maxValue, tab[i][j]);
}
}
System.out.println(maxValue);
}
}