ZOJ1117 POJ1521 HDU1053 Entropy,哈夫曼编码问题
来源:互联网 发布:新疆人口增长表格数据 编辑:程序博客网 时间:2024/06/14 13:23
经典的哈夫曼编码问题,贪心算法。
/******************************************************************************* # Author : Neo Fung # Email : neosfung@gmail.com # Last modified: 2011-12-29 19:36 # Filename: ZOJ1117 POJ1521 HDU1053 Entropy.cpp # Description : ******************************************************************************/// #include "stdafx.h"// #define DEBUG#include <fstream>#include <stdio.h>#include <iostream>#include <string.h>#include <string>#include <memory.h>#include <limits.h>#include <algorithm>#include <math.h>#include <numeric>#include <functional>#include <ctype.h>#define MAX 128using namespace std;struct NODE{ int weight,index; NODE *lhs,*rhs;}node[MAX*MAX];int n_count;int num[MAX];bool inline cmp(const NODE &lhs,const NODE &rhs){ return lhs.weight>rhs.weight;}void func(NODE *p_arry,const int &len){ if(len<2) return; sort(p_arry,p_arry+len,cmp); swap(p_arry[len-2],p_arry[n_count]); p_arry[len-2].lhs=&p_arry[len-1]; p_arry[len-2].rhs=&p_arry[n_count]; p_arry[len-2].weight=p_arry[len-1].weight+p_arry[n_count].weight; ++n_count;}int sum_len(const NODE *p,const int &len){ if(p->lhs==NULL) return p->weight*len; else { return sum_len(p->lhs,len+1) + sum_len(p->rhs,len+1); }}int main(void){#ifdef DEBUG freopen("C:/Users/neo/Desktop/stdin.txt","r",stdin); freopen("C:/Users/neo/Desktop/stdout.txt","w",stdout); #endif char str[MAX*MAX]; while(scanf("%s",str)) { if(!strcmp(str,"END")) break; memset(num,0,sizeof(num)); for(int i=0;i<strlen(str);++i) ++num[str[i]]; sort(num,num+MAX,greater<int>()); int len=0; for(;num[len];++len) { node[len].lhs=NULL; node[len].rhs=NULL; node[len].weight=num[len]; node[len].index=len; } if(len>1) { n_count=len; for(int i=len;i>1;--i) func(node,i); int ans=sum_len(&node[0],0); printf("%d %d %.1lf\n",strlen(str)*8,ans,double(strlen(str)*8)/double(ans)); } else printf("%d %d %.1lf\n",strlen(str)*8,num[0],double(strlen(str)*8)/double(num[0])); } return 0;}
- ZOJ1117 POJ1521 HDU1053 Entropy,哈夫曼编码问题
- HDU1053,POJ1521,ZOJ1117 Entropy 哈夫曼编码
- ZOJ1117 POJ1521 HDU1053 Huffman编码
- 【POJ1521】【HDU1053】Entropy 哈夫曼(Huffman)编码
- 【哈夫曼编码】HDU1053-Entropy
- hdu1053:Entropy 之霍夫曼编码
- HDU1053 Entropy(huffman编码)
- zoj1117 Entropy
- hdu1053 Entropy —— 赫夫曼编码
- poj1521 Entropy
- HDU1053 Entropy
- HDU1053--Entropy
- HDU1053-Entropy
- zoj1117 - Entropy(贪心)
- HDU1053 哈夫曼编码
- Entropy hdu1053 哈夫曼树
- hdu1053 Entropy(哈夫曼树应用)
- HDU1053 - Entropy (哈夫曼树)
- IOCP
- use_hash
- 创建新的动态链接库 (DLL) 项目
- I2C系统
- 在SQLServer里面判断各种存在的条件
- ZOJ1117 POJ1521 HDU1053 Entropy,哈夫曼编码问题
- Oracle快照原理及实现总结
- JAVA实现二维数组的转置
- android 布局查看工具 hierarchyViewer
- msm7227平台Linux I2C驱动分析(2.6.29)
- 2011年倒数第三天,下雪,第一次写博客
- sed,grep命令使用心得
- HDU2254 奥运 矩阵应用
- ibatis 插入,返回自增id,批处理