要求:求两个字符串的所有公共子串,如“abcdefgad”和“adefgwgewegad”的公共子串为“defg”、“ad”(子串必须是连续不重合的且长度大于1)
方法一
- 对于较短的那个字符串,假设其长度为n,依次找到它的长度为n, n-1, n-2....1的若干的子串;
- 若另外那个较长的字符串包含了较短字符串的某个子串,则找到了二者的最长公共子串;
- 将两个字符串都剔除掉最长公共子串,然后再寻找下一个最长公共子串;
/**
* 获取两个字符串中所有相同子串。
*/
public static list<string> maxsub1(string s1, string s2){
list<string> result = new arraylist<>();
string max="",min="";
max = (s1.length() > s2.length()) ? s1 : s2;
min = (max == s1) ? s2 : s1;
for(int x = 0; x < min.length(); x++){
for(int y = min.length() ; y > x ; y--){
string temp = min.substring(x, y);
if(max.contains(temp) && temp.length()>1){
result.add(temp);
min = getspittext(temp,min);
max = getspittext(temp,max);
break;
}
}
}
return result;
}
private static string getspittext(string temp,string text) {
integer start = text.indexof(temp);
integer end = start + temp.length();
string minstartpart = text.substring(0,start);
string minendpart = text.substring(end,text.length());
text = minstartpart + " " + minendpart;
return text;
}方法二
采用动态规划的思想:用一个二维矩阵来记录中间结果,矩阵的横坐标为字符串1的各个字符,矩阵的纵坐标为字符串2的各个字符。
举例说明:假设两个字符串分别为"bab"和"caba" (当然我们现在一眼就可以看出来最长公共子串是"ba"或"ab")
b a b
c 0 0 0
a 0 1 0
b 1 0 1
a 0 1 0
可以看出,矩阵的斜对角线最长的那个就对应着两个字符串的最长公共子串。
不过在二维矩阵上找最长的由1组成的斜对角线也是件麻烦费时的事,可以用下面的方法改进:当要在矩阵是填1时让它等于其左上角元素加1。
b a b
c 0 0 0
a 0 1 0
b 1 0 2
a 0 2 0
这样矩阵中的最大元素就是最长公共子串的长度。另外,在构造这个二维矩阵的过程中由于得出矩阵的某一行后其上一行就没用了,所以实际上在程序中可以用一维数组来代替这个矩阵。
/**
* 获取两个字符串中最长相同子串。
*/
public static list<string> maxsub2(string s1, string s2){
list<string> result = new arraylist<>();
string max="",min="";
max = (s1.length() > s2.length()) ? s1 : s2;
min = (max == s1) ? s2 : s1;
while (true){
string same = maxsubstring(max,min);
if (commontools.isempty(same)||same.length()==1)
break;
result.add(same);
min = getspittext(same,min);
max = getspittext(same,max);
}
return result;
}
// 求解两个字符号的最长公共子串
public static string maxsubstring(string strone, string strtwo){
// 参数检查
if(strone==null || strtwo == null){
return null;
}
if(strone.equals("") || strtwo.equals("")){
return null;
}
// 矩阵的横向长度
int len1 = strone.length();
// 矩阵的纵向长度
int len2 = strtwo.length();
// 保存矩阵的上一行
int[] topline = new int[len1];
// 保存矩阵的当前行
int[] currentline = new int[len1];
// 矩阵元素中的最大值
int maxlen = 0;
// 矩阵元素最大值出现在第几列
int pos = 0;
char ch = ' ';
for(int i=0; i<len2; i++){
ch = strtwo.charat(i);
// 遍历str1,填充当前行的数组
for(int j=0; j<len1; j++){
if( ch == strone.charat(j)){
// 如果当前处理的是矩阵的第一列,单独处理,因为其坐上角的元素不存在
if(j==0){
currentline[j] = 1;
} else{
currentline[j] = topline[j-1] + 1;
}
if(currentline[j] > maxlen){
maxlen = currentline[j];
pos = j;
}
}
}
// 将矩阵的当前行元素赋值给topline数组; 并清空currentline数组
for(int k=0; k<len1; k++){
topline[k] = currentline[k];
currentline[k] = 0;
}
// 或者采用下面的方法
// topline = currentline;
// currentline = new int[len1];
}
return strone.substring(pos-maxlen+1, pos+1);
}测试
public static void main(string[] args) throws ioexception {
// 输入
string str1 = "abcdefgad";
string str2 = "adefgwgewegad";
system.out.println("相同的字符: " );
system.out.println(maxsub1(str1,str2));
system.out.println(maxsub2(str1,str2));
}结果如下:
相同的字符:
[defg, ad]
[defg, ad]
到此这篇关于java实现求两个字符串的所有公共子串方法详解的文章就介绍到这了,更多相关java求两个字符串的公共子串内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论