简体   繁体   English

Perl:最长前缀匹配(字符串)的最佳方法

[英]Perl: best way to do longest prefix match (string)

I have a list of around 5000 words. 我有大约5000个单词的列表。 I want to find the longest prefix match among those words for a given word. 我想在给定单词的单词中找到最长的前缀匹配。 For example, in my list I have : 例如,在我的列表中,我有:

1
121
12234
20345
21345

Now If I search for 12134 the result will be 121 (longest match). 现在,如果我搜索12134,结果将是121(最长匹配)。 I know it can be done in different ways. 我知道可以用不同的方式完成。 But, what should be the most efficient way ? 但是,最有效的方法应该是什么?

#!/usr/bin/env perl

use strict;
use warnings;

my @prefixes = qw(
    1
    121
    12234
    20345
    21345
);

my $num = '12134';

my ($longest) = sort { length $b <=> length $a } grep { 0 == index $num, $_ } @prefixes;

print "$longest\n";

Outputs 输出

121

You can get the regex engine to do this for you. 您可以获取正则表达式引擎来为您执行此操作。 It should be very fast 应该很快

I hope it's obvious that the regex pattern needs to be built only once, and can then be used to find the longest prefix for any number of target strings 我希望很明显,只需要构建一次正则表达式模式,然后就可以使用它查找任意数量的目标字符串的最长前缀。

use strict;
use warnings;
use 5.010;

my @prefixes = qw/
    1
    121
    12234
    20345
    21345
/;

my $target = 12134;

my $re = join '|', sort { length $b <=> length $a } @prefixes;
$re = qr/(?:$re)/;

say $1 if $target =~ /^($re)/;

output 产量

121

Update 更新

Alternatively, the Tree::Trie module can be used to implement the trie search that the regex engine provides like this 另外, Tree::Trie模块可用于实现正则表达式引擎提供的trie搜索,如下所示

use strict;
use warnings;
use 5.010;

use Tree::Trie;

my @prefixes = qw/
    1
    121
    12234
    20345
    21345
/;

my $target = 12134;

my $trie = Tree::Trie->new({ deepsearch => 'prefix' });
$trie->add(@prefixes);

say scalar $trie->lookup($target);

The output, of course, is the same as that of the previous code 当然,输出与之前的代码相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM