• 求两个字符串最大公共字符串(听说是微软几年前面试题)


    两个字符串的最大公共子串,是一个程序员们常常考到和想到的题目,听讲是当年微软面试时要求做的一个程序,写一个返回两个任意字串中最大公共串的函数,即abcdef 和 qcfbcc 返回值为bc
     

    注:你要考虑到字符串中最大公共串相等的问题。
    例如
    dddabd123456abcdefeeeee
    234dddabcdegeeee

     
    输出:
    dddab
    abcde
    #!/usr/bin/perl  
    use strict;
    use warnings;
    use Data::Dumper;
    my %hash1;
    my %hash2;
    my @arr;
    my $str1 = 'aab12345678';
    my $str2 = 'ab1234yb1234567';
    $str1 =~ /(.*?)(?{$hash1{$1}=$1})(*F)/
        ;    #强制回朔,列举所有字符串,存入hash
    $str2 =~ /(.*?)(?{$hash2{$1}=$1})(*F)/;
    
    for ( keys %hash1 ) {
        my $k = $_;
        push @arr, $k if exists $hash2{$k};
    }
    my ( $max, $min ) = sort { length($b) cmp length($a) } @arr;
    for (@arr) {
        if ( length($_) == length($max) ) {
            print "$_
    ";
        }
    }
  • 相关阅读:
    C#构造函数
    C#析构函数
    C#常量
    C#属性
    checklistbox的用法
    2012快捷键
    查询ORACLE存储关联表
    UltraDropDown
    Linux常用命令大全(非常全!!!)
    infra 仪表盘效果
  • 原文地址:https://www.cnblogs.com/mcshell/p/5659513.html
Copyright © 2020-2023  润新知