Longest palindromic substrings: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: future-proof for 5.36, use new bitwise string operator) |
||
Line 692: | Line 692: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
The short one - find all palindromes with one regex. |
The short one - find all palindromes with one regex. |
||
<syntaxhighlight lang="perl"> |
<syntaxhighlight lang="perl">use strict; |
||
use strict; # https://rosettacode.org/wiki/Longest_palindromic_substrings |
|||
use warnings; |
use warnings; |
||
Line 716: | Line 714: | ||
Longest Palindrome For abaracadabra = aba ara aca ada |
Longest Palindrome For abaracadabra = aba ara aca ada |
||
</pre> |
</pre> |
||
⚫ | |||
⚫ | |||
⚫ | |||
use strict; # https://rosettacode.org/wiki/Longest_palindromic_substrings |
|||
⚫ | |||
use warnings; |
use warnings; |
||
use feature 'bitwise'; |
|||
#@ARGV = 'pi.dat'; # uncomment to use this file or add filename to command line |
#@ARGV = 'pi.dat'; # uncomment to use this file or add filename to command line |
||
Line 738: | Line 736: | ||
my $right = substr $forward, $i, $range; |
my $right = substr $forward, $i, $range; |
||
my $left = substr $backward, $length - $i, $range; |
my $left = substr $backward, $length - $i, $range; |
||
( $right ^ $left ) =~ /^\0\0+/ and # evens |
( $right ^. $left ) =~ /^\0\0+/ and # evens |
||
($len = 2 * length $&) >= $#best and |
($len = 2 * length $&) >= $#best and |
||
$best[ $len ]{substr $forward, $i - length $&, $len}++; |
$best[ $len ]{substr $forward, $i - length $&, $len}++; |
||
( $right ^ "\0" . $left ) =~ /^.(\0+)/ and # odds |
( $right ^. "\0" . $left ) =~ /^.(\0+)/ and # odds |
||
($len = 1 + 2 * length $1) >= $#best and |
($len = 1 + 2 * length $1) >= $#best and |
||
$best[ $len ]{substr $forward, $i - length $1, $len}++; |
$best[ $len ]{substr $forward, $i - length $1, $len}++; |