http://acm.fafu.edu.cn/problem.php?id=1255
//优先队列
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 505;
const int inf = 1<<30;
int root,len,pos;
char str[1000];
bool vis[260];
struct node
{
int ch;
double weight;
int id,parent,lch,rch;
bool operator < ( const node &k ) const
{
if( weight != k.weight )
return weight > k.weight;
else
return id > k.id;
}
}huffnod[maxn];
priority_queue<node>que;
void create_huffman_tree()
{
root = 255;
node p1,p2;
while ( !que.empty() )
{
p1 = que.top(); que.pop();
if( que.empty() )
break;
p2 = que.top(); que.pop();
root++;
huffnod[root].ch = root; huffnod[root].id = pos++;
huffnod[root].weight = p1.weight + p2.weight;
huffnod[root].lch = p1.ch; huffnod[root].rch = p2.ch;
huffnod[p1.ch].parent = huffnod[p2.ch].parent = root;
que.push( huffnod[root] );
}
huffnod[root].parent = -1;
}
void decode_process()
{
int pos,cnt;
for( int i = 0; i < len; i ++ )
{
string ans;
pos = str[i];
while( pos != -1 )
{
cnt = huffnod[pos].parent;
if( pos == huffnod[cnt].lch )
ans = '0' + ans;
else if( pos == huffnod[cnt].rch )
ans = '1' + ans;
pos = cnt;
}
cout<<ans;
}
puts("");
}
void init()
{
for( int i = 0; i < 256; i ++ )
{
huffnod[i].ch = i;
huffnod[i].weight = inf;
}
}
void readData()
{
char ch;
pos = 0,len = 0;
init();
while( scanf("%c",&ch) != EOF )
{
str[len++] = ch;
if( !vis[ch] )
{
vis[ch] = true;
huffnod[ch].weight = 0;
}
huffnod[ch].weight ++;
}
for( int i = 0; i < 256; i ++ )
{
if( huffnod[i].weight != inf )
{
huffnod[i].id = pos++;
que.push( huffnod[i] );
}
}
}
int main()
{
//freopen("huffman5.in","r",stdin);
readData();
if( que.size() == 1 )
{
node p = que.top();
if( p.weight != 1 )
puts("");
return 0;
}
if( que.empty() )
return 0;
create_huffman_tree();
decode_process();
return 0;
}
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 505;
const int inf = 1<<30;
int root,len,pos;
char str[1000];
int p1,p2;
struct node
{
int ch;
int weight;
int id,parent,lch,rch;
}huffnod[maxn];
void select_2_small( node *ht,int root )
{
int i;
p1 = -1; p2 = -1;
for( i = 0; i <= root; i ++ )
{
if( ht[i].parent == -1 && ht[i].weight != 0 )
{
if( p1 == -1 ) p1 = i;
else
{
p2 = i;
break;
}
}
}
if( p2 == -1 )
return;
if( ht[p1].weight > ht[p2].weight || ( ht[p1].weight == ht[p2].weight && ht[p1].id > ht[p2].id ) )
{
p1 = p1 + p2;
p2 = p1 - p2;
p1 = p1 - p2;
}
for( i = i+1; i <= root; i ++ )
{
if( ht[i].parent != -1 || ht[i].weight == 0 )
continue;
else if ( ht[i].weight < ht[p1].weight )
{
p2 = p1; p1 = i;
}
else if( ht[i].weight == ht[p1].weight )
{
if( ht[i].id < ht[p1].id )
{
p2 = p1; p1 = i;
}
else if( ht[i].weight < ht[p2].weight || ( ht[i].weight == ht[p2].weight && ht[i].id < ht[p2].id ) )
p2 = i;
}
else if( ht[i].weight < ht[p2].weight || ( ht[i].weight == ht[p2].weight && ht[i].id < ht[p2].id ) )
{
p2 = i;
}
}
}
void create_huffman_tree()
{
root = 255;
while ( 1 )
{
select_2_small( huffnod,root );
if( p2 == -1 )
break;
root++;
huffnod[root].ch = root; huffnod[root].id = pos++;
huffnod[root].weight = huffnod[p1].weight + huffnod[p2].weight;
huffnod[root].lch = p1; huffnod[root].rch = p2;
huffnod[p1].parent = huffnod[p2].parent = root;
}
}
void decode_process()
{
int pos,cnt;
for( int i = 0; i < len; i ++ )
{
string ans;
pos = str[i];
while( huffnod[pos].parent != -1 )
{
cnt = huffnod[pos].parent;
if( pos == huffnod[cnt].lch )
ans = '0' + ans;
else if( pos == huffnod[cnt].rch )
ans = '1' + ans;
pos = cnt;
}
cout<<ans;
}
puts("");
}
void init()
{
for( int i = 0; i < maxn; i ++ )
{
huffnod[i].ch = i;
huffnod[i].weight = 0;
huffnod[i].parent = -1;
}
}
void readData()
{
char ch;
pos = 0,len = 0;
init();
while( scanf("%c",&ch) != EOF )
{
str[len++] = ch;
huffnod[ch].weight ++;
}
for( int i = 0; i < 256; i ++ )
{
if( huffnod[i].weight != 0 )
{
huffnod[i].id = pos++;
}
}
}
int main()
{
//freopen("huffman3.in","r",stdin);
readData();
create_huffman_tree();
decode_process();
return 0;
}