Skip to content

Instantly share code, notes, and snippets.

@sng2c
Created April 19, 2013 14:49
Show Gist options
  • Save sng2c/5420833 to your computer and use it in GitHub Desktop.
Save sng2c/5420833 to your computer and use it in GitHub Desktop.
사전코딩. 참고 : http://altools.tistory.com/60
#!/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";
@sng2c
Copy link
Author

sng2c commented Apr 19, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment