辞書を構築する必要があり、トライを組みました。備忘のためにメモ。
ρ(∂_∂) ソースだよ☆
trie.pl:
ρ(∂_∂) ソースだよ☆
trie.pl:
$accept = "\t"; # Any character that never appears in keys sub add_trie_entry ($$$) { my ($trie, $arr, $val) = @_; foreach my $e (@$arr) { $trie = (defined $trie->{$e}) ? $trie->{$e} : ($trie->{$e} = { }); } $trie->{$accept} = $val; } sub print_trie ($$) { my ($trie, $depth) = @_; foreach my $c (sort keys %$trie) { if ($c eq $accept) { print '.' x $depth, "=>", $trie->{$c}, "\n"; } else { print '.' x $depth, $c, "\n"; print_trie( $trie->{$c}, $depth + 1); } } } # An example to show how to build a trie using string keys: foreach my $d (keys %dic) { my @ch = split( //, $d); add_trie_entry( \%trie, \@ch, $dic->{$d}); } print_trie( \%trie, 0);