summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-10-25 01:46:27 -0500
committerJesse Luehrs <doy@tozt.net>2012-10-25 01:46:27 -0500
commit96957c8c77edfbd82a6f426645d817f7334dbfb9 (patch)
treee6096fbd5da26de5a01729dc9b28c49d22ef52b6
parent330b38defb373c651f979d2bb0a6faa8b0a9cb48 (diff)
downloadrosalind-96957c8c77edfbd82a6f426645d817f7334dbfb9.tar.gz
rosalind-96957c8c77edfbd82a6f426645d817f7334dbfb9.zip
another solution
-rw-r--r--KMP.pl25
1 files changed, 25 insertions, 0 deletions
diff --git a/KMP.pl b/KMP.pl
new file mode 100644
index 0000000..7c80d82
--- /dev/null
+++ b/KMP.pl
@@ -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);