Paste: naive subpalindrome

Author: yac
Mode: factor
Date: Fri, 24 Jun 2011 18:31:08
Plain Text |
USING: kernel io sequences accessors math ;
IN: subpalindrome

TUPLE: sstate str len from to ;
: <sstate> ( str -- sstate ) dup length dup 0 swap sstate boa ;

: snext ( sstate -- next-sstate )
    dup [ to>> ] [ len>> ] bi >= 
        [ dup [ to>> ] [ from>> ] bi - 1 - >>to 0 >>from ]
        [ [ 1 + ] change-from  [ 1 + ] change-to ]
    if ;
: ssubstr ( sstate -- substr )
    [ from>> ] [ to>> ] [ str>> ] tri subseq ;

: palindrome? ( str -- ? ) dup reverse = ;
: find-subpal ( str -- spal )
    <sstate> [ dup ssubstr palindrome? not ] [ snext ] while
    ssubstr ;

contents find-subpal print

New Annotation

Summary:
Author:
Mode:
Body: