Longest common prefix: Difference between revisions
Content added Content deleted
(added Easylang) |
(Added XPL0 example.) |
||
Line 3,984: | Line 3,984: | ||
["prefix", "suffix"] = "" |
["prefix", "suffix"] = "" |
||
["foo", "foobar"] = "foo" |
["foo", "foobar"] = "foo" |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang "XPL0">proc LCP(N, R); \Show longest common prefix |
|||
int N; char R; |
|||
int I, J; |
|||
string 0; |
|||
[ChOut(0, ^"); |
|||
if N <= 0 then [ChOut(0, ^"); CrLf(0); return]; |
|||
for J:= 0 to -1>>1 do |
|||
[for I:= 0 to N-1 do |
|||
if R(I,J) # R(0,J) or R(0,J) = 0 then |
|||
[ChOut(0, ^"); CrLf(0); return]; |
|||
ChOut(0, R(0,J)); |
|||
]; |
|||
]; |
|||
[ |
|||
LCP(3, ["interspecies", "interstellar", "interstate"]); |
|||
LCP(2, ["throne", "throne"]); |
|||
LCP(2, ["throne", "dungeon"]); |
|||
LCP(3, ["throne", "", "throne"]); |
|||
LCP(1, ["cheese"]); |
|||
LCP(1, [""]); |
|||
LCP(0); |
|||
LCP(2, ["prefix", "suffix"]); |
|||
LCP(2, ["foo", "foobar"]); |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
"inters" |
|||
"throne" |
|||
"" |
|||
"" |
|||
"cheese" |
|||
"" |
|||
"" |
|||
"" |
|||
"foo" |
|||
</pre> |
</pre> |
||