1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Longest Common Substring

Discussion in 'C++' started by Haggard, Feb 5, 2010.

  1. Haggard

    Haggard New Member

    Joined:
    Feb 5, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi!

    I am looking for a solution the longest common substring problem. I have the code in C++. Where can i find some help to convert the code in Multi-Pascal because I need a code which is for parallel processing.

    Thanks.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    int main () {
            
            while(1) {
                    
                    string first, second, lcsub, max;
                    
                    cout << "Enter two words" << endl;
                    cin >> first >> second;
                    if(cin.eof()) {
                            return 0;
                    }
                    for (int i=0; i < first.length(); i++){
                                    for (int j=0; j < second.length(); j++){
                                            for (int k=1; k <= first.length() && k <= second.length(); k++){
                                                    if (first.substr(i,k) == second.substr(j,k)){
                                                            lcsub = first.substr(i,k);
                                                    }
                                                    else{
                                                            if (lcsub.length() > max.length())
                                                                    max=lcsub;
                                                            lcsub="";
                                                    }
                                            }
                                                    if (lcsub.length() > max.length())
                                                                    max=lcsub;
                                                    lcsub="";
                                    }
                    }
                    cout << "Longest Common Substring: " << max << endl << endl;
            }
            return 0;
    }
    
     

Share This Page