diff options
author | Jesse Luehrs <doy@tozt.net> | 2012-10-25 01:46:27 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2012-10-25 01:46:27 -0500 |
commit | 96957c8c77edfbd82a6f426645d817f7334dbfb9 (patch) | |
tree | e6096fbd5da26de5a01729dc9b28c49d22ef52b6 | |
parent | 330b38defb373c651f979d2bb0a6faa8b0a9cb48 (diff) | |
download | rosalind-96957c8c77edfbd82a6f426645d817f7334dbfb9.tar.gz rosalind-96957c8c77edfbd82a6f426645d817f7334dbfb9.zip |
another solution
-rw-r--r-- | KMP.pl | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.016; + +chomp(my $str = <>); + +my @failure = (0); + +my $idx = 0; +for my $start (1..(length($str) - 1)) { + while (1) { + if (substr($str, $start, 1) eq substr($str, $idx, 1)) { + ++$idx; + last; + } + else { + last if $idx == 0; + $idx = $failure[$idx - 1]; + } + } + push @failure, $idx; +} + +say join(' ', @failure); |