Closed Thread
Results 1 to 3 of 3

Thread: Longest Common Substring

  1. #1
    Haggard is offline Newbie
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Longest Common Substring

    Hi!

    I am looking for a solution of the longest common substring problem.
    I have the code but in C++. Can someone help me 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;
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Longest Common Substring

    You can do parallel processing in C++.
    That said, what part of the algorithm are you having trouble with? What do you have so far on the Pascal side?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: Longest Common Substring

    Here is my conversion. I dont know what "Multi-pascal" or how it would apply to this function.

    Code:
    function LongestCommonSubstring(const AFirst, ASecond: String): String;
    var
      I, J, K: Integer;
      LSubString: String;
    begin
      Result := '';
      for I := 1 to Length(AFirst) do for J := 1 to Length(ASecond) do
      begin
        K := 1;
        while (K <= Length(AFirst)) and (K <= Length(ASecond)) do
        begin
          if Copy(AFirst, I, K) = Copy(ASecond, J, K) then
          begin
            LSubString := Copy(AFirst, I, K);
          end else
          begin
            if Length(LSubString) > Length(Result) then Result := LSubString;
            LSubString := '';
          end;
          if Length(LSubString) > Length(Result) then Result := LSubString;
          LSubString := '';
          Inc(K);
        end;
      end;
    end;

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Finding numbers having unique products for each substring of it's digits
    By fayyazlodhi in forum CSharp Tutorials
    Replies: 0
    Last Post: 05-22-2011, 08:34 AM
  2. [C++] finding substring in string
    By Amonijack in forum C and C++
    Replies: 7
    Last Post: 01-11-2011, 06:59 PM
  3. Reading longest word within a string.
    By psycho in forum Pascal and Delphi
    Replies: 5
    Last Post: 04-18-2010, 06:45 PM
  4. The longest initializer ever!
    By Aereshaa in forum C and C++
    Replies: 4
    Last Post: 12-22-2009, 05:16 PM
  5. Longest on the internet??
    By etraffic in forum The Lounge
    Replies: 13
    Last Post: 07-15-2006, 10:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts