Created
April 19, 2013 14:49
-
-
Save sng2c/5420833 to your computer and use it in GitHub Desktop.
사전코딩. 참고 : http://altools.tistory.com/60
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use 5.010; | |
use YAML; | |
=pod | |
w = NIL; | |
add all possible charcodes to the dictionary | |
for (every character c in the uncompressed data) do | |
if ((w + c) exists in the dictionary) then | |
w = w + c; | |
else | |
add (w + c) to the dictionary; | |
add the dictionary code for w to output; | |
w = c; | |
endif | |
done | |
add the dictionary code for w to output; | |
display output; | |
=cut | |
sub makeDic{ | |
my @out; | |
my @data = @_; | |
my %map; | |
map{ $map{$_}=0 }@data; | |
my $w = ''; | |
foreach my $c (@data){ | |
if( defined($map{$w.$c}) ){ | |
$w = $w.$c; | |
} | |
else{ | |
$map{$w.$c}=0; | |
push(@out,$w); | |
$w = $c; | |
} | |
} | |
push(@out,$w); | |
return @out; | |
} | |
my @data = qw( D A B C E A B C E A B C E D ); | |
my @out = makeDic(@data); | |
say "@data"; | |
say "@out"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output:
D A B C E A B C E A B C E D
D A B C E AB CE ABC E D