use strict; use warnings; use String::LCSS_XS qw( lcss ); my @array1 = ('This sentence is stored in array1') x 1_000; my @array2 = ( 'Thank you for looking on this.', 'This sentence is a test sentence.', 'This sentence is stored in array2', 'This is an other sentence.', 'All variables are stored in array2', ) x 20_000; preprocess( \@array1 ); preprocess( \@array2 ); process( \@array1, \@array2 ); exit 0; my %words; my %codes; my $word_num; sub process { my ( $array1, $array2 ) = @_; for my $elem1 (@$array1) { my $best_subseq = ""; my $best_subseq_words = 0; my $best_subseq_chars = 0; my $found = 0; for my $elem2 (@$array2) { my $subseq = lcss( $elem1->[1], $elem2->[1] ); if ( $subseq && length($subseq) > $best_subseq_words ) { my $real_subseq = join ' ', map $codes{$_}, split //, $subseq; if ( length($real_subseq) > $best_subseq_chars ) { $best_subseq = $real_subseq; $best_subseq_words = length($subseq); $best_subseq_chars = length($real_subseq); $found = 1; } } } if ( $found == 1 ) { printf "<<%s>> is the lcss of\n\t<<%s>>\n", $best_subseq, $elem1->[0]; } } } sub preprocess { my ($array) = @_; for my $i ( 0 .. $#$array ) { my $packed = join '', map { my $code = $words{$_}; if ( not defined $code ) { $code = chr( $word_num += 1 ); $words{$_} = $code; $codes{$code} = $_; } $code; } $array->[$i] =~ /\w+/g; $array->[$i] = [ $array->[$i], $packed ]; } }