当前位置 : 主页 > 编程语言 > java >

找出两个字符串中最大公共子字符串,如

来源:互联网 收集:自由互联 发布时间:2022-07-20
import java.util.ArrayList; import java.util.List;//找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad" public class ZuiDaZiChuan { public static void main(String args[]) { String str1 = "abccade";


import java.util.ArrayList;
import java.util.List;//找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
public class ZuiDaZiChuan { public static void main(String args[]) {
String str1 = "abccade";
String str2 = "dgcadde"; List<String> str1Sub = new ArrayList<String>();
List<String> str2Sub = new ArrayList<String>(); List<String> listSame = new ArrayList<String>();
for (int i = 0; i <= str1.length(); i++) {
for (int j = i; j <= str1.length(); j++) {
str1Sub.add(str1.substring(i, j));
}
} for (int i = 0; i <= str2.length(); i++) {
for (int j = i; j <= str2.length(); j++) {
str2Sub.add(str2.substring(i, j));
}
} for (int i = 0; i < str1Sub.size(); i++) {
for (int j = 0; j < str2Sub.size(); j++) {
if (str1Sub.get(i).equals(str2Sub.get(j))) {
listSame.add(str1Sub.get(i));
}
}
} int maxId = 0;
int maxValue = 0;
for (int i = 0; i < listSame.size(); i++) {
if (listSame.get(i).length() > maxValue) {
maxId = i;
maxValue = listSame.get(i).length();
} }
System.out.print(listSame.get(maxId));
}
}
上一篇:java 传送文件
下一篇:没有了
网友评论