Keep It Simple, Stupid PerlMonks

### Re: Finding longest palindrome from a string

by BrowserUk (Pope)
 on Aug 13, 2004 at 12:57 UTC ( #382644=note: print w/replies, xml ) Need Help??

in reply to Finding longest palindrome from a string

 ```#! perl -slw use strict; sub buk { my \$string = shift; my @pals; while( \$string =~ m[(.) (?=( (?:\1) | (?:.\1) ) ) ]gx ){ my( \$left, \$right ) = ( \$-[ 0 ], \$+[ -1 ] ); while( \$left and \$right < length( \$string ) and substr( \$string, \$left, 1 ) eq substr( \$string, \$right, 1 +) ) { \$left--; \$right++ ; } my \$pal = substr( \$string, \$left, \$right - \$left ); if( !@pals or length( \$pals[ -1 ] ) < length( \$pal ) ) { @pals = \$pal; } else { push @pals, \$pal unless @pals; } } return wantarray ? \$pals[ 0 ] : @pals; } print \$ARGV[ 0 ]; print ' ' x index( \$ARGV[ 0 ], \$_ ), \$_ for buk \$ARGV[ 0 ]; __END__ P:\test>382567 1111111121111111111112111111111111111111111112111111111 +111211111 1111111121111111111112111111111111111111111112111111111111211111 11111121111111111112111111111111111111111112111111111111211111 P:\test>382567 1111111121111111111112111111111111111111111112111111111 +1121111 11111111211111111111121111111111111111111111121111111111121111 111111111111211111111111111111111111211111111111 [download]```

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Create A New User
Node Status?
node history
Node Type: note [id://382644]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2018-01-18 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (206 votes). Check out past polls.

Notices?