Longest common substring: Difference between revisions

m (syntax highlighting fixup automation)
Line 1,560:
<pre>test</pre>
 
(*)=={{header|Pascal}}==
{{trans|Java}}
=== using FreePascal ===
{{trans|Delphi}}
{{works with|Free Pascal|version 3.2.0 }}
{{works with|Free Pascal| 3.2.2 }}
<syntaxhighlight lang="pascal">
<syntaxhighlight lang="pascal">(*)
PROGRAM LongestCommonSubString.pas;
 
{$WARN 6058 off : Call to subroutine "$1" marked as inline is not inlined} // Use for variants
{$DEFINE DEBUGGING}
 
{$IFDEF FPC}
{$mode objfpc}{$H+}{$J-}{$m+}{$R+}{$T+}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
 
(*)
 
Free Pascal Compiler version 3.2.02 [20202022/0608/1401] for x86_64
 
The free and readable alternative at C/C++ speeds
compiles natively to almost any platform, including raspberry PI *
Line 1,582 ⟶ 1,584:
 
https://www.freepascal.org/advantage.var
(*)
 
FUNCTION lcss( S1, S2: string ) : string ;
(*)
LongestCommonSubString: plain version without extra libraries
Version without `USES SysUtils, Variants ;` and without `SubStr`, we do not need it here...
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
 
FUNCTION IFF ( Cond: boolean; A, B: string ) : string ;
FOR j := 0 TO LenS2 DO
BEGIN
BEGIN IF ( Cond ) THEN IFF := A ELSE IFF := B ; END ;
 
x := 0;
 
FUNCTION WHILE lcss( S1 [ i + x ] =, S2: [string j) +: x ] )string DO;
VAR
j : Integer = 0 ;
 
k : Integer = 0 ;
BEGIN
Inc ( x ) ;
IF ( ( ( i + x ) > LenS1 ) or ( ( j + x ) > LenS2 ) ) THEN BREAK ;
END ;
 
S : string = '' ;
IF ( x > Max ) THEN
BEGIN
lcss := '' ;
 
FOR j := 0 TO length ( S1 ) DO
BEGIN
Max := x ;
BEGIN
Start := i ;
END ;
FOR k := length ( S1 ) - j DOWNTO 1 DO
END ;
END ;
BEGIN
 
S := S1 [ ( j + 1 ) lcss := copy.. ( S1,k Start,+ ( Startj + Max1 ) )] ;
IF ( pos ( S, S2 ) > 0 ) AND
( length ( S ) > length ( lcss ) ) THEN
BEGIN
lcss := S ;
BREAK ;
END ;
 
END ;
 
END;
VAR
S1: string ;
S2: string ;
 
END;
BEGIN
 
S1 := 'thisisatest' ;
S2 := 'testing123testing' ;
WriteLn( Lcss ( S1, S2 ) ) ;
END.
 
</syntaxhighlight>JPD 2021/06/18
VAR
Output:
 
S1: string = 'thisisatest' ;
test
 
S2: string = 'testing123isatesting' ;
=== using FreePascal v.2 ===
{{works with|Free Pascal|version 3.2.0 }}
<syntaxhighlight lang="pascal">
PROGRAM LongestCommonSubString.pas;
 
{$IFDEF FPC}
{$mode objfpc}{$H+}{$J-}{R+}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF}
 
BEGIN
(*)
 
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
 
https://www.freepascal.org/advantage.var
(*)
 
USES
 
SysUtils;
 
FUNCTION lcss( S1, S2: string ) : string ;
IF ParamCount = 2 THEN
(*)
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 ;
 
S1 := IFF( ( ParamStr ( 1 ) > '' ), ParamStr ( 1 ) , S1 );
BEGIN
 
S2 := IFF( ( ParamStr ( 2 ) > '' ), ParamStr ( 2 ) , S1 );
 
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 ) ) ;
Writeln ( 'string B = ', S2 ) ;
END.
</syntaxhighlight>JPD 2021/06/18
WriteLn ( Lcss ( S1, S2 ) ) ;
END.
(*)
</syntaxhighlight>
<PRE>JPD 2021/06/18
Output:
 
Line 1,742 ⟶ 1,674:
 
test
</PRE>(*)
 
=={{header|Perl}}==
122

edits