Longest common substring: Difference between revisions

Line 1,385:
=={{header|Pascal}}==
{{trans|Java}}
=== using FreePascal ===
{{works with|FreePascal|version 3.2.0 }}
<lang Pascal>
PROGRAM lcss.pas;
 
{$DEFINE DEBUGGING}
{$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
 
(*)
 
FUNCTION LCSS( S1, S2: string ) : string ;
(*)
LongestCommonSubString: plain version without extra libraries
From https://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Longest_common_substring
translated from java
(*)
 
VAR
i: integer = 0;
j: integer = 0;
x: integer = 0;
LenS1: integer = 0;
LenS2: integer = 0;
Start: integer = 0;
Max: integer = 0;
BEGIN
LenS1 := length ( S1 ) - 1 ;
LenS2 := length ( S2 ) - 1 ;
FOR i := 0 TO LenS1 DO
BEGIN
 
FOR j := 0 TO LenS2 DO
BEGIN
 
x := 0;
 
WHILE ( S1 [ i + x ] = S2 [ j + x ] ) DO
 
BEGIN
Inc ( x ) ;
IF ( ( ( i + x ) > LenS1 ) or ( ( j + x ) > LenS2 ) ) THEN BREAK ;
END ;
 
IF ( x > Max ) THEN
 
BEGIN
Max := x ;
Start := i ;
END ;
END ;
END ;
LCSS := copy ( S1, Start, ( Start + Max ) ) ;
//LCSS := copy ( S1, 1, Start ) ;
 
END ;
 
VAR
S1: string ;
S2: string ;
 
BEGIN
 
S1 := 'thisisatest' ;
S2 := 'testing123testing' ;
WriteLn( Lcss ( S1, S2 ) ) ;
END.
 
</lang>JPD 2021/06/18
Output:
test
 
=={{header|Perl}}==
122

edits