From 3bac0ba740ef9c37aeedcb2b9e8e82daf1af1d51 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 25 Oct 2012 00:14:33 -0500 Subject: another solution --- LCS.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 LCS.pl diff --git a/LCS.pl b/LCS.pl new file mode 100644 index 0000000..a52be7f --- /dev/null +++ b/LCS.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.016; + +use List::MoreUtils 'all'; + +chomp(my @strings = <>); + +my $min = length($strings[0]); +my $min_idx = 0; +for my $i (1..$#strings) { + if (length($strings[$i]) < $min) { + $min = length($strings[$i]); + $min_idx = $i; + } +} +my $str = splice(@strings, $min_idx, 1); + +LENGTH: for my $len (reverse 1..length($str)) { + for my $start (0..(length($str) - $len)) { + my $substr = substr($str, $start, $len); + if (all { /$substr/ } @strings) { + say $substr; + last LENGTH; + } + } +} -- cgit v1.2.3