How to find Longest Common Substring using C++

40,207

Solution 1

Here is an excellent article on finding all common substrings efficiently, with examples in C. This may be overkill if you need just the longest, but it may be easier to understand than the general articles about suffix trees.

Solution 2

The answer is GENERALISED SUFFIX TREE. http://en.wikipedia.org/wiki/Generalised_suffix_tree

You can build a generalised suffix tree with multiple string.

Look at this http://en.wikipedia.org/wiki/Longest_common_substring_problem

The Suffix tree can be built in O(n) time for each string, k*O(n) in total. K is total number of strings.

So it's very quick to solve this problem.

Solution 3

This is a dynamic programming problem and can be solved in O(mn) time, where m is the length of one string and n is of other.

Like any other problem solved using Dynamic Programming, we will divide the problem into subproblem. Lets say if two strings are x1x2x3....xm and y1y2y3...yn

S(i,j) is the longest common string for x1x2x3...xi and y1y2y3....yj, then

S(i,j) = max { length of longest common substring ending at xi/yj, if ( x[i] == y[j] ), S(i-1, j-1), S(i, j-1), S(i-1, j) }

Here is working program in Java. I am sure you can convert it to C++.:

public class LongestCommonSubstring {

    public static void main(String[] args) {
        String str1 = "abcdefgijkl";
        String str2 = "mnopabgijkw";
        System.out.println(getLongestCommonSubstring(str1,str2));
    }

    public static String getLongestCommonSubstring(String str1, String str2) {
        //Note this longest[][] is a standard auxialry memory space used in Dynamic
                //programming approach to save results of subproblems. 
                //These results are then used to calculate the results for bigger problems
        int[][] longest = new int[str2.length() + 1][str1.length() + 1];
        int min_index = 0, max_index = 0;

                //When one string is of zero length, then longest common substring length is 0
        for(int idx = 0; idx < str1.length() + 1; idx++) {
            longest[0][idx] = 0;
        }

        for(int idx = 0; idx < str2.length() + 1; idx++) {
            longest[idx][0] = 0;
        }

        for(int i = 0; i <  str2.length(); i++) {
            for(int j = 0; j < str1.length(); j++) {

                int tmp_min = j, tmp_max = j, tmp_offset = 0;

                if(str2.charAt(i) == str1.charAt(j)) {
                    //Find length of longest common substring ending at i/j
                    while(tmp_offset <= i && tmp_offset <= j &&
                            str2.charAt(i - tmp_offset) == str1.charAt(j - tmp_offset)) {

                        tmp_min--;
                        tmp_offset++;

                    }
                }
                //tmp_min will at this moment contain either < i,j value or the index that does not match
                //So increment it to the index that matches.
                tmp_min++;

                //Length of longest common substring ending at i/j
                int length = tmp_max - tmp_min + 1;
                //Find the longest between S(i-1,j), S(i-1,j-1), S(i, j-1)
                int tmp_max_length = Math.max(longest[i][j], Math.max(longest[i+1][j], longest[i][j+1]));

                if(length > tmp_max_length) {
                    min_index = tmp_min;
                    max_index = tmp_max;
                    longest[i+1][j+1] = length;
                } else {
                    longest[i+1][j+1] = tmp_max_length;
                }


            }
        }

        return str1.substring(min_index, max_index >= str1.length() - 1 ? str1.length() - 1 : max_index + 1);
    }
}

Solution 4

There is a very elegant Dynamic Programming solution to this.

Let LCSuff[i][j] be the longest common suffix between X[1..m] and Y[1..n]. We have two cases here:

  • X[i] == Y[j], that means we can extend the longest common suffix between X[i-1] and Y[j-1]. Thus LCSuff[i][j] = LCSuff[i-1][j-1] + 1 in this case.

  • X[i] != Y[j], since the last characters themselves are different, X[1..i] and Y[1..j] can't have a common suffix. Hence, LCSuff[i][j] = 0 in this case.

We now need to check maximal of these longest common suffixes.

So, LCSubstr(X,Y) = max(LCSuff(i,j)), where 1<=i<=m and 1<=j<=n

The algorithm pretty much writes itself now.

string LCSubstr(string x, string y){
    int m = x.length(), n=y.length();

    int LCSuff[m][n];

    for(int j=0; j<=n; j++)
        LCSuff[0][j] = 0;
    for(int i=0; i<=m; i++)
        LCSuff[i][0] = 0;

    for(int i=1; i<=m; i++){
        for(int j=1; j<=n; j++){
            if(x[i-1] == y[j-1])
                LCSuff[i][j] = LCSuff[i-1][j-1] + 1;
            else
                LCSuff[i][j] = 0;
        }
    }

    string longest = "";
    for(int i=1; i<=m; i++){
        for(int j=1; j<=n; j++){
            if(LCSuff[i][j] > longest.length())
                longest = x.substr((i-LCSuff[i][j]+1) -1, LCSuff[i][j]);
        }
    }
    return longest;
}
Share:
40,207
David Gomes
Author by

David Gomes

Software Engineer who is really interested in UX. Graduated in Software Engineering in Portugal. Currently @memsql, previously @Unbabel.

Updated on July 09, 2022

Comments

  • David Gomes
    David Gomes almost 2 years

    I searched online for a C++ Longest Common Substring implementation but failed to find a decent one. I need a LCS algorithm that returns the substring itself, so it's not just LCS.

    I was wondering, though, about how I can do this between multiple strings.

    My idea was to check the longest one between 2 strings, and then go check all the others, but this is a very slow process which requires managing many long strings on the memory, making my program quite slow.

    Any idea of how this can be speeded up for multiple strings? Thank you.

    Important Edit One of the variables I'm given determines the number of strings the longest common substring needs to be in, so I can be given 10 strings, and find the LCS of them all (K=10), or LCS of 4 of them, but I'm not told which 4, I have to find the best 4.

  • Saeed Amiri
    Saeed Amiri about 12 years
    Would you clarify your answer, I really can't understand it, what's your result for: abdac, aabbdc, aac?
  • David Gomes
    David Gomes about 12 years
    Thank you! Any link on suffix trees and C++? I have never used them, so I need to learn.
  • Yu Hao
    Yu Hao over 10 years
    Looks like Java, while this question is tagged with C++.
  • snegi
    snegi over 10 years
    It should be fairly easy to port it to C++ language, there isnt anything being used that is specific to Java and is not in C++
  • vikingsteve
    vikingsteve over 10 years
    Snegi, I found this useful, thank you very much. YuHao, care to port it to C++ yourself?
  • chowey
    chowey about 9 years
    Very simple article on suffix arrays, suitable for this problem. Got me up and running quickly. Very nice.
  • Ankesh Anand
    Ankesh Anand almost 9 years
    I think you ended up describing the solution to the Longest Common Subsequence problem.
  • SexyBeast
    SexyBeast about 8 years
    Can you explain the last part where you find out the common substring?
  • Quirk
    Quirk about 7 years
    @SexyBeast We know that LCS[i][j] gives the length of the longest common suffix ending at index i-1 for string x and ending at index j-1 for string y. So finding the common suffix is just a matter of getting a suffix of length LCS[i][j] from either of the strings. The answer above chooses to use the first string x to that effect.
  • recodeFuture
    recodeFuture about 6 years
    There's a bug in the code above, but edit is not possible (due to rules on min possible size of edit). LCSuff should be of size (m+1, n+1) not (m, n). The bottom part could also easily be improved by keeping track of max current substring length and start/end of the substring in one of the strings. The string can than be extracted as e.g. x.substr(start_substr, len_substr).