对字符串HELL0_HULU中的字符进行二进制编码,使得字符串的编码长度尽可能短,最短长度为?
来源:互联网 发布:数据库开发与dba 编辑:程序博客网 时间:2024/05/18 13:42
这个题主要考察的是对haffman树的解法。先给出代码,因为其中还有点小错误,但是影响不大。
#include <stdio.h>#include<string.h>#include<stdlib.h>typedef struct HuffmanNode{unsigned int weight;int parent,lchild,rchild;}HuffmanNode,*HuffmanTree;typedef char ** HuffmanCode;char *count(char *input,int *temp){//统计单个字母出现的顺序int i;int len=strlen(input);int hash[256]={0};int count=0;for(i=0;i<len;i++){hash[input[i]]++;}for(i=0;i<256;i++)//统计出现的字符个数{if(hash[i])count++;}char *output=(char*)malloc(count+1);count=0;for(i=0;i<256;i++) //存储出现的字符{if(hash[i]!=0){output[count++]=i;*temp=hash[i];temp++;}}output[count]='\0';return output;}int min(HuffmanTree *HT,int n){//找到其中最小的一个int i=0;unsigned int k=1000;int flag;while((*HT)[i].parent!=0)i++;for(;i<=n;i++)if((*HT)[i].parent==0&&(*HT)[i].weight<k){k=(*HT)[i].weight;flag=i;}(*HT)[flag].parent=1;return flag;}void select(HuffmanTree *HT,int i,int *s1,int *s2){//找到权值最小的两个元素的下标*s1=min(HT,i);*s2=min(HT,i);if((*s1)>(*s2)){int temp=*s1;*s1=*s2;*s2=temp;}}void get_huffman_code(HuffmanTree *HT,HuffmanCode *HC,int n,int *w){int m;int s1,s2;int i;int c,f;m=2*n-1;*HT=(HuffmanNode*)malloc(sizeof(HuffmanNode)*(m+1));HuffmanNode *p;p=*HT+1;for(i=1;i<=n;i++){p->weight=*w;p->parent=0;p->lchild=0;p->rchild=0;w++;p++;}for(;i<=m;i++){p->weight=0;p->parent=0;p->lchild=0;p->rchild=0;p++;}for(i=n+1;i<=m;i++){select(HT,i-1,&s1,&s2);(*HT)[i].weight=(*HT)[s1].weight+(*HT)[s2].weight;(*HT)[i].lchild=s1;(*HT)[i].rchild=s2;(*HT)[s1].parent=(*HT)[s2].parent=i;}*HC=(char**)malloc(sizeof(char*)*(n+1));char *temp=(char*)malloc(sizeof(n));int start;temp[n-1]='\0';for(i=1;i<=n;i++){start=n-1;for(c=i,f=(*HT)[i].parent;f!=0;c=f,f=(*HT)[f].parent){if((*HT)[f].lchild==c)temp[--start]='0';else if((*HT)[f].rchild==c)temp[--start]='1';}(*HC)[i]=(char*)malloc(sizeof(n-start));strcpy((*HC)[i],temp+start);}}void main(){int hash[256]={0};char string[]="hello_hulu";char *p=count(string,hash);printf("%s\n",p);HuffmanTree HT;HuffmanCode HC;int n=strlen(p);get_huffman_code(&HT,&HC,n,hash);for(int i=1;i<=n;i++) int length=0;for(i=0;i<n;i++)length+=strlen(HC[i+1])*hash[i];printf("%d\n",length);}
运行结果如图:
- 对字符串HELL0_HULU中的字符进行二进制编码,使得字符串的编码长度尽可能短,最短长度为?
- 设计一个算法,给一个字符串进行二进制编码,使得编码后的字符串长度最短
- 请设计一个算法,给一个字符串进行二进制编码,使得编码后字符串的长度最短。
- 请设计一个算法,给一个字符串进行二进制编码,使得编码后字符串的长度最短。(哈弗曼编码)
- XMU 1315.字符串编码 字符串的编码最短长度
- 给定两个字符串和一个字典,从头到尾找到最短变换序列的长度,使得:一次只能改变一个字符每个中间词必须存在于字典中
- 棒的最短长度
- 用二进制来编码字符串"adceadaa",需要能够相据编码,解码回原来的字符串,则至少需要二进制字符的长度是?
- 如何理解用信息熵来表示最短的平均编码长度
- 一个字符串中包含另一个字符串所有字符的最短子串长度?——《编程之美》最短摘要的生成的简化
- 搜索树中的最短长度问题
- 【DP】三角形中的最短路径长度
- 对字符串进行编码
- smartConfig---通过数据包长度位作为编码位,进行短数据通讯
- 关于"密码最短长度为7,其中必须包含以下非字母数字字符: 1”的问题
- 密码最短长度为 7,其中必须包含以下非字母数字字符: 1
- "密码最短长度为7,其中必须包含以下非字母数字字符1"解决方法
- "密码最短长度为7,其中必须包含以下非字母数字字符1"解决方法
- 前端性能的测试与优化
- 江泽民向柬埔寨太后致唁电哀悼西哈努克去世-西哈努克太皇-柬埔寨独立-西哈努克
- Android 资源目录的相关知识
- win32修改.exe图标
- ALSA(lib和util)交叉编译与测试
- 对字符串HELL0_HULU中的字符进行二进制编码,使得字符串的编码长度尽可能短,最短长度为?
- CSDN高校俱乐部招募公告
- oracle复杂查询练习题
- Tika
- 类似于选择城市相同的首字母放在一起的效果
- 人脸识别网上资源列表(装载)
- java实现简单的单点登录
- 不同风格的游戏技术人 - Game Developer stereotypes
- 表的数据插入相关操作(identity,临时表,获取表列)