Java 不剪切单词的最长公共子字符串

Java 不剪切单词的最长公共子字符串,java,algorithm,longest-substring,Java,Algorithm,Longest Substring,我对编程非常陌生,我正在尝试解决Java中最长的常见序列/子串问题之一。所以我正在研究的算法问题是在不切割单词的情况下找到最长的公共子串 例如:给定string1=他有3便士和5个25美分,string2=q3nniesp应该返回便士 其他示例:string1=他们命名了新的place face cafe和string2=e face,输出将是e face cafe 我试图找出这个算法,但我无法决定是否需要将它们转换为字符数组或将其作为字符串计算。两个字符串都有空格的方式让我很困惑 我遵循了一些

我对编程非常陌生,我正在尝试解决Java中最长的常见序列/子串问题之一。所以我正在研究的算法问题是在不切割单词的情况下找到最长的公共子串

例如:给定
string1=他有3便士和5个25美分
string2=q3nniesp
应该返回
便士

其他示例:
string1=他们命名了新的place face cafe
string2=e face
,输出将是
e face cafe

我试图找出这个算法,但我无法决定是否需要将它们转换为字符数组或将其作为字符串计算。两个字符串都有空格的方式让我很困惑

我遵循了一些现有的stackoverflow问题,并试图从
https://www.geeksforgeeks.org/

static String findLongestSubsequence(String str1, String str2) {

        char[] A = str1.toCharArray();
        char[] B = str2.toCharArray();
        if (A == null || B == null) return null;

        final int n = A.length;
        final int m = B.length;

        if (n == 0 || m == 0) return null;

        int[][] dp = new int[n+1][m+1];

        // Suppose A = a1a2..an-1an and B = b1b2..bn-1bn
        for (int i = 1; i <= n; i++ ) {
            for (int j = 1; j <= m; j++) {

                // If ends match the LCS(a1a2..an-1an, b1b2..bn-1bn) = LCS(a1a2..an-1, b1b2..bn-1) + 1
                if (A[i-1] == B[j-1]) dp[i][j] = dp[i-1][j-1] + 1;

                    // If the ends do not match the LCS of a1a2..an-1an and b1b2..bn-1bn is
                    // max( LCS(a1a2..an-1, b1b2..bn-1bn), LCS(a1a2..an-1an, b1b2..bn-1) )
                else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);

            }
        }

        int lcsLen = dp[n][m];
        char[] lcs = new char[lcsLen];
        int index = 0;

        // Backtrack to find a LCS. We search for the cells
        // where we included an element which are those with
        // dp[i][j] != dp[i-1][j] and dp[i][j] != dp[i][j-1])
        int i = n, j = m;
        while (i >= 1 && j >= 1) {

            int v = dp[i][j];

            // The order of these may output different LCSs
            while(i > 1 && dp[i-1][j] == v) i--;
            while(j > 1 && dp[i][j-1] == v) j--;

            // Make sure there is a match before adding
            if (v > 0) lcs[lcsLen - index++ - 1] = A[i-1]; // or B[j-1];

            i--; j--;

        }

        return new String(lcs, 0, lcsLen);
    }
静态字符串findlongest子序列(字符串str1、字符串str2){
char[]A=str1.toCharArray();
char[]B=str2.toCharArray();
如果(A==null | | B==null)返回null;
最终整数n=A.长度;
最终int m=B.长度;
如果(n==0 | | m==0)返回null;
int[][]dp=新int[n+1][m+1];
//假设A=a1a2..an-1an和B=b1b2..bn-1bn
对于(int i=1;i=1){
int v=dp[i][j];
//这些命令可能输出不同的LCS
而(i>1&&dp[i-1][j]==v)i--;
而(j>1&&dp[i][j-1]==v)j--;
//添加前确保存在匹配项
如果(v>0)lcs[lcsLen-index++-1]=A[i-1];//或B[j-1];
我--;j--;
}
返回新字符串(lcs,0,lcsLen);
}

但我总是得到错误的输出。例如,第一次输出给出了
output=3nnies
,我真的被困在这一点上了,有人能帮我一把或稍微修改一下算法吗?谢谢大家。

我试用了您原来的算法,不幸的是,它没有朝正确的方向发展

我假设以下准则适用:

  • 匹配的子字符串包含来自给定子字符串的字符,这些字符的顺序可能不正确
  • 给定子字符串中的字符可能会在匹配子字符串中出现多次
一、 因此,在使用java streams时,我们冒昧地使用了暴力算法:

// complexity of O(N*M), where N is the length of the original string and M is the length of the substring
static String longestCommonSequence(String string, String sub) {
    List<Character> primaryMatch = new ArrayList<>();
    List<Character> secondaryMatch = new ArrayList<>();
    // N iterations loop on original string
    for(char c : string.toCharArray()) {
      // M iterations loop on the substring
      if(sub.indexOf(c) != -1) {
        primaryMatch.add(c);
      }
      else {
        if(!primaryMatch.isEmpty()) {
          // replace secondaryMatch content with the current longet match
          if(secondaryMatch.size() <= primaryMatch.size()) {
            secondaryMatch.clear();
            secondaryMatch.addAll(primaryMatch);
          }
          primaryMatch.clear();
        }
      }
    }
    if(primaryMatch.size() < secondaryMatch.size()) {
      return secondaryMatch.stream().map(String::valueOf).collect(Collectors.joining());
    }
    return primaryMatch.stream().map(String::valueOf).collect(Collectors.joining());
}
注意第二个输出中的差异-根据您描述的输出行为,上述算法的结果是正确的,因为
ace face cafe
e face cafe
长,因为允许从给定子字符串多次使用字符

请注意算法中的一个微妙问题:
if(secondaryMatch.size()子字符串:子字符串是字符串中连续的字符序列,顺序很重要。因此,string1=他有3个便士和5个四分之一。string2=q3nniesp它应该返回nnies?它应该返回便士,似乎它不必是连续的……这非常有效,感谢您的时间和关注。@Interstar:hap我想帮助巴迪
string1 = He had 3 pennies and 5 quarters and string2 = q3nniesp ---> pennies
string1 = They named the new place face cafe and string2 = e face ---> ace face cafe