- 论坛徽章:
- 0
|
- #!/usr/bin/perl
- open STR1, '/opt/str1.txt';
- open STR2, '/opt/str2.txt';
- our $min;
- our $max;
- our $sum;
- $str1 = <STR1>;
- $str2 = <STR2>;
- $len = length($str1);
- $max = $len;
- $min = 0;
- $point = int( $max / 2 );
- &match($point);
- sub match {
- my ($point) = @_;
- $end = $len - $point;
- for $begin ( 0 .. $end ) {
- $lcs= substr( $str1, $begin, $point );
- if ( $str2 =~ /$lcs/ ) {
- $aver = $max - $min;
- if ( $aver <= 1 ) {
- print $lcs;
- exit;
- }
- else {
- $min = $point;
- $point = $min + int( ( $max - $min ) / 2 );
- &match($point);
- }
- }
- }
- $max = $point;
- $point = $max - int( ( $max - $min + 1 ) / 2 );
- &match($point);
- }
复制代码 二分法
如果公共子串较长有奇效 |
|