SUFARY.pm で Longest Common Prefix Search
2007-05-15-5
[Programming][Algorithm]
sufary の Perl モジュールである SUFARY.pm を使って
longest common prefix search をするサンプル。
コード:
実行例:
ref.
- [を] 正規表現でCommon Prefix Search[2007-05-15-1]
longest common prefix search をするサンプル。
コード:
use strict; use warnings; use SUFARY; my $wordset_fn = shift @ARGV; my $suf = SUFARY->new($wordset_fn); while (<>) { chomp; next if /^\s*$/; my @chars = split(//, $_); for (my $i = 0; $i < @chars; $i++) { my $longest; my $key; for (my $j = $i; $j < @chars; $j++) { $key .= $chars[$j]; my ($left, $right) = $suf->range_search($key); last if not defined $left and not defined $right; ($left, $right) = $suf->range_search($key."\n"); next if not defined $left and not defined $right; if ($right - $left == 0) { my $li = $suf->get_position($right); $longest = $suf->get_line($li); } } print $longest if defined $longest; } }
実行例:
% cat a.txt pen th this lov love % mkary -q -l a.txt % echo 'this is a pen which I love' | ./lcps.pl a.txt this pen love
ref.
- [を] 正規表現でCommon Prefix Search[2007-05-15-1]