文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>微软面试题 最大公共串 求两个字符串的最大公共子..

微软面试题 最大公共串 求两个字符串的最大公共子..

时间:2010-07-22  来源:黑色阳光_cu


#!/bin/env perl


use strict;
use warnings;

my $s1 = 'xcfadfasdfdfdfdfdfdfdfdfdfasf34lk343434343433333fdasfd';
my $s2 = 'dadfdasdfdfdfdfdfdfdfdfdf3434343434343jfshasdazlzlasbfasf';

warn &max_mutual_str($s1, $s2);

sub max_mutual_str
{
    my ($cnt_offest, $cnt_length) = (0, 1);
    my ($substr_offest, $substr_length) = (0, 0);
    my $str_length = length($_[0]);

    while ($str_length - $cnt_offest > $substr_length)
    {
        if (index($_[1], substr($_[0], $cnt_offest, $cnt_length)) == -1)
        {
            ++$cnt_offest;
        }
        else
        {
            if ($substr_length < $cnt_length)
            {
                ($substr_offest, $substr_length) = ($cnt_offest, $cnt_length);
            }
            
            ++$cnt_length;
        }
    }

    return substr($_[0], $substr_offest, $substr_length);
}


相关阅读 更多 +
排行榜 更多 +
我要当超人

我要当超人

休闲益智 下载
幸存者的命运

幸存者的命运

飞行射击 下载
精英战区3d

精英战区3d

飞行射击 下载