Longest palindromic substrings: Difference between revisions

From Rosetta Code
Content added Content deleted
m (added related task)
(→‎{{header|REXX}}: re-wrote REXX program to handle multiple strings for examination.)
Line 48: Line 48:
=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds and displays the longest palindromic string(s) in a given string. */
<lang rexx>/*REXX program finds and displays the longest palindromic string(s) in a given string. */
parse arg s . /*obtain optional argument from the CL.*/
parse arg s /*obtain optional argument from the CL.*/
if s=='' | s=="," then s= 'babaccd' /*Not specified? Then use the default.*/
if s=='' | s=="," then s= 'babaccd rotator reverse forever several palindrome abaracadaraba'

L= length(s) /*obtain length of the specified string*/
longest= 0 /*longest palindrome found (so far). */
do i=1 for words(s); x= word(s, i) /*obtain a string to be examined. */
@.= /*initialize all possible lists to null*/
L= length(x) /*obtain length of the specified string*/
do j=1 for L /*search for palindromes in the string.*/
longest= 0 /*longest palindrome found (so far). */
do k=j to L; LL= k - j + 1 /*obtain length of possible palindromes.*/
@.= /*initialize all possible lists to null*/
do j=1 for L /*search for palindroms in the string S*/
do k=j to L; LL= k - j + 1 /*obtain length of possible palindroms.*/
if LL<longest then iterate /*Palindrome length<max? Then skip it.*/
if LL<longest then iterate /*Palindrome length<max? Then skip it.*/
$= substr(s, j, LL) /*obtain a possible palindromic substr.*/
$= substr(x, j, LL) /*obtain a possible palindromic substr.*/
if $\==reverse($) then iterate /*Not a palindrome? Then skip it.*/
if $\==reverse($) then iterate /*Not a palindrome? Then skip it.*/
longest= max(longest, LL) /*set the longest palindrome to this L.*/
longest= max(longest, LL) /*set the longest palindrome to this L.*/
Line 62: Line 64:
end /*k*/
end /*k*/
end /*j*/
end /*j*/
say
say ' longest palindromic substrings for string: ' x
say '────────────────────────────────────────────'copies('─', 2+length(x))


do n=1 for words(@.longest) /*show longest palindromic substrings. */
if longest==0 then do; say 'no longest palindromic string was found for: ' s; exit 13
end
say ' longest palindromic substrings for string: ' s /*show title.*/
say '────────────────────────────────────────────'copies('─', 2+length(s)) /*show sep. */

do n=1 for words(@.longest) /*show longest palindromic substrings. */
/*show pals. */
/*show pals. */
say ' (length='longest") " word(@.longest, n)
say ' (length='longest") " word(@.longest, n)
end /*n*/
end /*n*/ /*stick a fork in it, we're all done. */</lang>
end /*i*/ /*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 77: Line 78:
─────────────────────────────────────────────────────
─────────────────────────────────────────────────────
(length=3) bab
(length=3) bab
(length=3) aba

longest palindromic substrings for string: rotator
─────────────────────────────────────────────────────
(length=7) rotator

longest palindromic substrings for string: reverse
─────────────────────────────────────────────────────
(length=5) rever

longest palindromic substrings for string: forever
─────────────────────────────────────────────────────
(length=5) rever

longest palindromic substrings for string: several
─────────────────────────────────────────────────────
(length=3) eve

longest palindromic substrings for string: palindrome
────────────────────────────────────────────────────────
(length=1) p
(length=1) a
(length=1) l
(length=1) i
(length=1) n
(length=1) d
(length=1) r
(length=1) o
(length=1) m
(length=1) e

longest palindromic substrings for string: abaracadaraba
───────────────────────────────────────────────────────────
(length=3) aba
(length=3) ara
(length=3) aca
(length=3) ada
(length=3) ara
(length=3) aba
(length=3) aba
</pre>
</pre>

Revision as of 17:27, 28 September 2020

Longest palindromic substrings is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Let given a string s. The goal is to find the longest palindromic substring in s.

Related tasks



Phix

<lang Phix>function longest_palindromes(string s) -- s = lower/strip_spaces_and_punctuation/utf8_to_utf32, if rqd

   integer longest = 2 -- (do not treat length 1 as palindromic)
   sequence res = {}
   for i=1 to length(s) do
       for e=length(s) to i+longest-1 by -1 do
           if s[e]=s[i] then
               string p = s[i..e]
               integer lp = length(p)
               if lp>=longest and p=reverse(p) then
                   if lp>longest then
                       longest = lp
                       res = {p}
                   elsif not find(p,res) then
                       res = append(res,p)
                   end if
               end if
           end if
       end for
   end for
   return res

end function

constant tests = {"babaccd","rotator","reverse","forever","several","palindrome","abaracadaraba"} for i=1 to length(tests) do

   printf(1,"%s: %v\n",{tests[i],longest_palindromes(tests[i])})

end for</lang>

Output:
babaccd: {"bab","aba"}
rotator: {"rotator"}
reverse: {"rever"}
forever: {"rever"}
several: {"eve"}
palindrome: {}
abaracadaraba: {"aba","ara","aca","ada"}

REXX

<lang rexx>/*REXX program finds and displays the longest palindromic string(s) in a given string. */ parse arg s /*obtain optional argument from the CL.*/ if s== | s=="," then s= 'babaccd rotator reverse forever several palindrome abaracadaraba'

     do i=1  for words(s);    x= word(s, i)     /*obtain a string to be examined.      */
     L= length(x)                               /*obtain length of the specified string*/
     longest= 0                                 /*longest palindrome found  (so far).  */
     @.=                                        /*initialize all possible lists to null*/
          do j=1   for L                        /*search for palindroms in the string S*/
            do k=j  to L;      LL= k - j + 1    /*obtain length of possible palindroms.*/
            if LL<longest      then iterate     /*Palindrome length<max?  Then skip it.*/
            $= substr(x, j, LL)                 /*obtain a possible palindromic substr.*/
            if $\==reverse($)  then iterate     /*Not a palindrome?       Then skip it.*/
            longest= max(longest, LL)           /*set the longest palindrome to this L.*/
            @.LL= @.LL $                        /*add a palindromic substring to a list*/
            end   /*k*/
          end     /*j*/
     say
     say ' longest palindromic substrings for string: '                     x
     say '────────────────────────────────────────────'copies('─', 2+length(x))
          do n=1  for words(@.longest)          /*show longest palindromic substrings. */
             /*show pals. */
          say '    (length='longest")   "   word(@.longest, n)
          end     /*n*/
     end          /*i*/                         /*stick a fork in it,  we're all done. */</lang>
output   when using the default input:
 longest palindromic substrings for string:  babaccd
─────────────────────────────────────────────────────
    (length=3)    bab
    (length=3)    aba

 longest palindromic substrings for string:  rotator
─────────────────────────────────────────────────────
    (length=7)    rotator

 longest palindromic substrings for string:  reverse
─────────────────────────────────────────────────────
    (length=5)    rever

 longest palindromic substrings for string:  forever
─────────────────────────────────────────────────────
    (length=5)    rever

 longest palindromic substrings for string:  several
─────────────────────────────────────────────────────
    (length=3)    eve

 longest palindromic substrings for string:  palindrome
────────────────────────────────────────────────────────
    (length=1)    p
    (length=1)    a
    (length=1)    l
    (length=1)    i
    (length=1)    n
    (length=1)    d
    (length=1)    r
    (length=1)    o
    (length=1)    m
    (length=1)    e

 longest palindromic substrings for string:  abaracadaraba
───────────────────────────────────────────────────────────
    (length=3)    aba
    (length=3)    ara
    (length=3)    aca
    (length=3)    ada
    (length=3)    ara
    (length=3)    aba

Ring

<lang ring> load "stdlib.ring"

st = "babaccd" palList = []

for n = 1 to len(st)-1

   for m = n+1 to len(st)
       sub = substr(st,n,m-n)
       if ispalindrome(sub) and len(sub) > 1
          add(palList,[sub,len(sub)])
       ok
   next

next

palList = sort(palList,2) palList = reverse(palList) resList = [] add(resList,palList[1][1])

for n = 2 to len(palList)

   if palList[1][2] = palList[n][2]
      add(resList,palList[n][1])
   ok

next

see "Input: " + st + nl see "Longest palindromic substrings:" + nl see resList </lang>

Output:
Input: babaccd
Longest palindromic substrings:
bab
aba