两个字符串的最大公共子串,是一个程序员们经常考到和想到的题目,听讲是当年微软面试时要求作的一个程序,写一个返回两个任意字串中最大公共串的函数,即abcdef 和 qcfbcc 返回值为bc
程序员
- #!/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 "$_\n";
- }
- }
output:web
b1234567面试