Longest palindromic substrings: Difference between revisions

Content added Content deleted
(→‎faster: bugfix)
Line 385: Line 385:
sequence res = {}
sequence res = {}
for i=1 to length(s) do
for i=1 to length(s) do
integer rev = iff(i>1 and s[i-1]=s[i]?1:0),
for j=0 to iff(i>1 and s[i-1]=s[i]?1:0) do
fwd = 0
integer rev = j,
while rev<i and i+fwd<=length(s) and s[i-rev]=s[i+fwd] do
fwd = 0
rev += 1
while rev<i and i+fwd<=length(s) and s[i-rev]=s[i+fwd] do
fwd += 1
rev += 1
end while
fwd += 1
string p = s[i-rev+1..i+fwd-1]
end while
integer lp = length(p)
string p = s[i-rev+1..i+fwd-1]
if lp>=longest then
integer lp = length(p)
if lp>longest then
if lp>=longest then
longest = lp
if lp>longest then
res = {p}
longest = lp
elsif not find(p,res) then -- (or just "else")
res = {p}
res = append(res,p)
elsif not find(p,res) then -- (or just "else")
res = append(res,p)
end if
end if
end if
end if
end for
end for
end for
return res -- (or "sort(res)" or "unique(res)", as needed)
return res -- (or "sort(res)" or "unique(res)", as needed)
end function
end function


printf(1,"%s: %v\n",{s,longest_palindromes_raku(piStr)})</lang>
printf(1,"%s: %v\n",{s,longest_palindromes_raku(piStr)})
s = "abbbc"
printf(1,"%s: %v\n",{s,longest_palindromes_raku(s)})</lang>
{{out}}
(first line matches the above, the second was a initially a bug)
<pre>
3.141592653589793238...05600101655256375679 (10,002 digits): {"398989893","020141020"}
abbbc: {"bbb"}
</pre>


=={{header|Python}}==
=={{header|Python}}==