Longest common substring: Difference between revisions

Line 1,474:
</lang>JPD 2021/06/18
Output:
 
test
 
=== using FreePascal v.2 ===
{{works with|FreePascal|version 3.2.0 }}
<lang Pascal>
PROGRAM LongestCommonSubString.pas;
 
{$IFDEF FPC}
{$mode objfpc}{$H+}{$J-}{R+}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
 
(*)
 
Free Pascal Compiler version 3.2.0 [2020/06/14] for x86_64
The free and readable alternative at C/C++ speeds
compiles natively to almost any platform, including raspberry PI *
Can run independently from DELPHI / Lazarus
 
(*)
 
USES
 
SysUtils;
 
FUNCTION lcss( S1, S2: string ) : string ;
(*)
LongestCommonSubString: adaptation from Delphi
 
(*)
VAR
j: Integer = 0 ;
k: Integer = 0 ;
lenS1: integer = 0 ;
S: string = '' ;
BEGIN
Result := S ;
lenS1 := S1.Length ;
FOR j := 0 TO lenS1 - 1 DO
BEGIN
FOR k := lenS1 - j DOWNTO 0 DO
BEGIN
S := S1.Substring ( j, k ) ;
IF ( S2.IndexOf ( S ) > -1 ) AND ( S.Length > Result.Length ) THEN
Result := S;
END;
END;
END;
VAR
S1: string ;
S2: string ;
 
BEGIN
 
S1 := 'thisisatest' ;
S2 := 'testing123testing' ;
IF ParamCount = 2 THEN
BEGIN
IF NOT ParamStr(1).IsEmpty THEN
S1 := ParamStr(1);
IF NOT ParamStr(2).IsEmpty THEN
S2 := ParamStr(2);
END;
Writeln ('string A = ', S1) ;
Writeln ('string B = ', S2) ;
WriteLn ( Lcss ( S1, S2 ) ) ;
END.
</lang>JPD 2021/06/18
Output:
 
string A = thisisatest
 
string B = testing123testing
 
test
122

edits