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 |
||
for j=0 to iff(i>1 and s[i-1]=s[i]?1:0) do |
|||
integer rev = j, |
|||
fwd = 0 |
|||
rev += |
while rev<i and i+fwd<=length(s) and s[i-rev]=s[i+fwd] do |
||
rev += 1 |
|||
fwd += 1 |
|||
end while |
|||
string p = s[i-rev+1..i+fwd-1] |
|||
integer lp = length(p) |
|||
if lp>longest then |
if lp>=longest then |
||
if lp>longest then |
|||
longest = lp |
|||
res = {p} |
|||
elsif not find(p,res) then -- (or just "else") |
|||
res = append(res,p) |
|||
end if |
|||
end if |
end if |
||
end |
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)}) |
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}}== |