n个字符串的最长公共子字符串的Java实现

n个字符串的最长公共子字符串的Java实现,java,longest-substring,Java,Longest Substring,我需要找到n个字符串中最长的公共子字符串,并在我的项目中使用结果 java中是否有任何现有的实现/库已经做到了这一点?页面提供了许多语言所需的内容 public static int longestSubstr(String first, String second) { if (first == null || second == null || first.length() == 0 || second.length() == 0) { return 0; }

我需要找到n个字符串中最长的公共子字符串,并在我的项目中使用结果

java中是否有任何现有的实现/库已经做到了这一点?

页面提供了许多语言所需的内容

public static int longestSubstr(String first, String second) {
    if (first == null || second == null || first.length() == 0 || second.length() == 0) {
        return 0;
    }

    int maxLen = 0;
    int fl = first.length();
    int sl = second.length();
    int[][] table = new int[fl][sl];

    for (int i = 0; i < fl; i++) {
        for (int j = 0; j < sl; j++) {
            if (first.charAt(i) == second.charAt(j)) {
                if (i == 0 || j == 0) {
                    table[i][j] = 1;
                }
                else {
                    table[i][j] = table[i - 1][j - 1] + 1;
                }
                if (table[i][j] > maxLen) {
                    maxLen = table[i][j];
                }
            }
        }
    }
    return maxLen;
}
public static int longestSubstr(字符串第一,字符串第二){
if(first==null | | second==null | | first.length()==0 | | second.length()==0){
返回0;
}
int maxLen=0;
int fl=first.length();
int sl=second.length();
int[][]表=新的int[fl][sl];
对于(int i=0;imaxLen){
maxLen=表[i][j];
}
}
}
}
返回maxLen;
}

您可以尝试将wikipedia()的版本扩展为n个字符串,方法是将其放入一个循环中,循环遍历所有字符串。

我们可以使用下面的代码识别n个字符串中最长的公共子字符串

public static String identifyCommonSubStrOfNStr(String [] strArr){

    String commonStr="";
    String smallStr ="";        

    //identify smallest String      
    for (String s :strArr) {
        if(smallStr.length()< s.length()){
            smallStr=s;
        }
    }

    String tempCom="";
    char [] smallStrChars=smallStr.toCharArray();               
    for (char c: smallStrChars){
        tempCom+= c;

        for (String s :strArr){
            if(!s.contains(tempCom)){
                tempCom=c;
                for (String s :strAarr){
                    if(!s.contains(tempCom)){
                        tempCom="";
                        break;
                    }
                }
                break;
            }               
        }

        if(tempCom!="" && tempCom.length()>commonStr.length()){
            commonStr=tempCom;  
        }                       
    }   

    return commonStr;
}
公共静态字符串identificationCommonSubstrofnStr(字符串[]strArr){
字符串commonStr=“”;
字符串smallStr=“”;
//识别最小的字符串
用于(字符串s:strArr){
if(smallStr.length()commonStr.length()){
commonStr=tempCom;
}                       
}   
返回commonStr;
}
那怎么办

这是一个小型(约100KB)图书馆,位于Maven Central。该算法使用基数树和后缀树的组合。已知其具有线性时间复杂度()

public静态字符串getLongestCommonSubstring(集合字符串){
LCSubstringSolver=新的LCSubstringSolver(新的DefaultCharSequenceNodeFactory());
用于(字符串s:字符串){
求解器。添加(s);
}
返回解算器.getLongestCommonSubstring().toString();
}
公共字符串findLongestCommonSubstring(字符串源,字符串目标){
int table[][]=newint[source.length()+1][dest.length()+1];
for(int col=0;col<表[0]。长度;col++){
表[0][col]=0;
}
for(int row=0;row最大值){
最大值=表[行][列];
索引=行;
}
}否则{
表[行][col]=0;
}
}
}
返回source.substring(index-max,index);
}

这可能会有帮助@Baadshah-its用于两个字符串。。。我需要n stringsCheck的实现此链接用于两个子字符串。。。它需要n个子字符串的值。。。假设您有s1=“aa111”s2=“aa221”s3=“1”,s1和s2的lcs是aa,aa和s3的lcs是“”,但正确的答案是“1”@user1628340您可以使用上面的代码并迭代运行它。如果您有一个N元素数组,只需运行代码N-1次,在0,1对上运行它;1,2; 2,3; 3,4; ... 直到你做完为止。@sigpwned那也不行。如果有s1=“--a.txt”,s2=“--b.txt”,s3=“--c.swe”。。。s1和s2的lcs为s4=.txt,s2和s3的lcs id为s5=“--”。。。在下一次迭代中,s4和s5的lcs为“”,而正确答案为“-”啊,是的。您需要跟踪每对的所有公共子字符串,并在运行时计算交点。从最后一组中选择最长的字符串;那是你的lcs。循环行不通!假设您有s1=“aa111”s2=“aa221”s3=“1”,s1和s2的lcs是aa,aa和s3的lcs是“”,但正确答案是“1”。迭代应在原始字符串上进行,而不是在其他比较的临时结果上进行。。。然后你得到正确的解决方案!那也不行。如果有s1=“--a.txt”,s2=“--b.txt”,s3=“--c.swe”。。。s1和s2的lcs为s4=.txt,s2和s3的lcs id为s5=“--”。。。在下一次迭代中,s4和s5的lcs为“”,而正确答案为“-”欢迎使用堆栈溢出!虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
public static String getLongestCommonSubstring(Collection<String> strings) {
    LCSubstringSolver solver = new LCSubstringSolver(new DefaultCharSequenceNodeFactory());
    for (String s: strings) {
        solver.add(s);
    }
    return solver.getLongestCommonSubstring().toString();
}
public String findLongestCommonSubstring(String source, String dest) {
    int table[][] = new int[source.length() + 1][dest.length() + 1];
    for (int col = 0; col < table[0].length; col++) {
        table[0][col] = 0;
    }

    for (int row = 0; row < table.length; row++) {
        table[row][0] = 0;
    }

    int max = 0;
    int index = 0;
    for (int row = 1; row < table.length; row++) {
        char sourceChar = source.charAt(row - 1);
        for (int col = 1; col < table[row].length; col++) {
            char destChar = dest.charAt(col - 1);
            if (sourceChar == destChar) {
                table[row][col] = table[row - 1][col - 1] + 1;
                if (table[row][col] > max) {
                    max = table[row][col];
                    index = row;
                }
            } else {
                table[row][col] = 0;
            }
        }
    }
    return source.substring(index - max, index);
}