- 论坛徽章:
- 0
|
今天早上看自己的博客,又看到这个代码,突然发现,当初竟然犯傻了,既然都找到最大的公共字符串了,干嘛还要在遍历一遍散列呢,真的够傻的,惭愧啊。
#!perl
sub max_mutual_str {
my($str1,$str2) = @_;
my %cmp;
my $max;
my $beginning_station; #beginning station of the common string in $str1
my @list1=split '',$str1;
my @list2=split '',$str2;
for my $i (0..$#list1){
for my $j (0..$#list2){
$cmp{$i.$j}=($list1[$i] eq $list2[$j]) ? (1+$cmp{($i-1).($j-1)}) : 0;
if ($cmp{$i.$j}>$max){
$max=$cmp{$i.$j};
$beginning_station=$i+1-$max;
}
print "$cmp{$i.$j}";
}
print "\n";
}
return substr($str1,$beginning_station,$max);
}
my ($str1,$str2) = @ARGV;
my $str= max_mutual_str($str1,$str2);
print "$str\n"; |
|
|