- 论坛徽章:
- 0
|
#!d:/perl64/bin/perl
use strict;
use warnings;
#match max string
sub m_m_s{
my $max='';
#string1's position,string2's position
my $p1=0;
my $p2=0;
#length of s2
my $l2;
my ($s1,$s2)=split(',',$_);
if($s1&&$s2){
$l2=length($s2);
my $l1=length($s1);
if ($l1>$l2) {
($s1,$s2) = ($s2,$s1);
($l1,$l2) = ($l2,$l1);
}
foreach my $j (0..($l1-1)) {
foreach $p2 (0..($l2-1)) {
while (substr($s1,$p1,1) eq substr($s2,$p2,1)) {
if ($p1 eq ($l1-1)) {
$p1+=1;
last;
}
$p1++;$p2++;
}
unless($p1 eq $j){
if (length($max)<($p1-$j)) {
$max=substr($s1,$j,$p1-$j);
}
}
$p1=$j;
}
}
if ($max eq '') {
$max.='no match!';
}
}else {
print "please input two string separated with comma!"
}
return $max;
}
print 'hello> ';
while (<stdin>) {
chomp $_;
#enter q or Q to quit!
if (/[qQ]/) {
exit 0;
}
print m_m_s($_),"\nhello> ";
}
很菜,别见笑 |
|