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; }
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?
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;
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks