RosettaCodeData/Task/Longest-common-substring/Icon/longest-common-substring-1.icon
2026-04-30 12:34:36 -04:00

21 lines
846 B
Text

procedure lcs(s1,s2) # return longest common substring of s1 and s2
if *s1 > *s2 then s1 :=: s2 # s1 is the shorter string
return s1 ? if j := *s1 to 1 by -1 & # Return substring, s, of s1 that is
tab(1 to *s1) & # also a substring of s2. Evaluate s from
find(s := move(j),s2) then s else "" # longest to shortest.
end
procedure main(A)
lcstest("thisisatest","testing123testing")
lcstest("abracadabra","arabracae")
lcstest("abc","123")
lcstest("testing","sting")
lcstest("thisisatest_stinger","testing123testingthing")
lcstest("thisisatest_stinger","thisis")
lcstest("testing123testingthing","thisis")
lcstest("thisisatest","thisisatest")
end
procedure lcstest(s1,s2)
write("\"",s1,"\" and ","\"",s2,"\" --> \"",lcs(s1,s2),"\"")
end