This function would return all the longest consecutive words between these two sentnces. The problem is right now I have two lists of over 4000 sentnces to compare, while using this function is extremly slow. Is there a way to accelerate this function? thanks in advance. Zhesub lc_substr { my ($s, $t) = @_; my $z = 0; my @S = (undef, split(/ /, $s)); my @T = (undef, split(/ /, $t)); my $m = scalar @S; my $n = scalar @T; my @LCS = (); my @start = (); my @end = (); for my $i ( 1 .. $m ) { for my $j ( 1 .. $n ) { if ($S[$i] =~ m/^$T[$j]$/ig && $S[$i] ne undef && $T[$j] ne unde +f &&$S[$i] ne " "&&$T[$j] ne " ") { $L[$i-1][$j-1] ||= 0; $L[$i][$j] = $L[$i-1][$j-1] + 1; if ($L[$i][$j] > $z) { $z = $L[$i][$j]; @ret = (); @start = (); @end = (); @prod = (); } if ($L[$i][$j] == $z) { $start = $i-$z+1; $end = $i; push(@start,$start); push(@end,$end); push(@prod,$t); } } } } $num = scalar @start; for ($n = 0; $n < $num;$n++){ if($prod[$n] ne ""){ $str = $prod[$n]." "."[".$start[$n].",".$end[$n]."]"; push (@LCS, $str); } } return @LCS; }
In reply to LCS efficiency problem by zhe
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |