哈夫曼译码

1000(ms)

10000(kb)

1974 / 4142

通常要求根据给定的编码本对密文进行解码。现已给定相应字符的哈夫曼编码,要求根据编码对密文进行解码。(建立哈夫曼树以及编码、主函数等都已经给出,你只需要填写译码函数void ccode(haffnode  hafftree[],int n)即可。

const int maxvalue=100;

const int maxbit=100;

const int maxn=100;

#include "iostream"

#include "stdio.h"

#include "stdlib.h"

using namespace std;

struct haffnode

{

char ch;

int weight;

int flag;

int parent;

int leftchild;

int rightchild;

};

struct code

{

int bit[maxn];

int start;

int weight;

char ch;

};

void haffman(int weight[],char text[],int n,haffnode hafftree[])

{

int j,m1,m2,x1,x2,i;

for(i=0;i< 2*n-1;i++)

{

if(i < n)

{

hafftree[i].weight=weight[i];

hafftree[i].

ch=text[i];

}

else

{

hafftree[i].weight=0;

hafftree[i].ch='#';

}

hafftree[i].parent=0;

hafftree[i].flag=0;

hafftree[i].leftchild=-1;

hafftree[i].rightchild=-1;

}

for(i=0;i< n-1;i++)

{

m1=m2=maxvalue;

x1=x2=0;

for(j=0;j< n+i;j++)

{

if(hafftree[j].weight< m1&&hafftree[j].flag==0)

{

m2=m1;

x2=x1;

m1=hafftree[j].weight;

x1=j;

}

else if(hafftree[j].weight< m2&&hafftree[j].flag==0)

{

m2=hafftree[j].weight; x2=j;

}

}

hafftree[x1].parent=n+i;

hafftree[x2].parent=n+i;

hafftree[x1].flag=1;

hafftree[x2].flag=1;

hafftree[n+i].weight=hafftree[x1].weight+hafftree[x2].weight;

hafftree[n+i].leftchild=x1; hafftree[n+i].rightchild=x2;

}

}

void haffmancode(haffnode hafftree[],int n,code haffcode[])

{

code cd; int i,j; int child,parent;

for( i=0;i< n;i++)

{

cd.start=n-1;

cd.weight=hafftree[i].weight;

cd.ch=hafftree[i].ch;

child=i;

parent=hafftree[child].parent;

while(parent!=0)

{

if(hafftree[parent].leftchild==child)

cd.bit[cd.start]=0;

else cd.bit[cd.start]=1;

cd.start--;

child=parent;

parent=hafftree[child].parent;

}

for(j=cd.start+1;j< n;j++)

haffcode[i].bit[j]=cd.bit[j];

haffcode[i].start=cd.start;

haffcode[i].weight=cd.weight;

haffcode[i].ch=cd.ch;

}

}

void ccode(haffnode hafftree[],int n)

{ }

int main( )

{

int n=8;

int weight[]={5,29,7,8,14,23,3,11};

char text[]={'a','b','c','d','e','f','g','h'};

haffnode myhafftree[maxvalue];

code myhaffcode[maxvalue];

haffman(weight,text,n,myhafftree);

haffmancode(myhafftree,n,myhaffcode);

ccode(myhafftree,n);

return 0;

}

输入

 

根据哈夫曼树编码表,针对字符串做好的编码结果。

输出

 

对每一行需要解码的串,进行解码,并输出解码后的结果。

样例输入

000100011011101110

样例输出

aabcc
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
const int maxvalue=100;
const int maxbit=100;
const int maxn=100;
struct haffnode
{char ch; int weight; int flag; int parent;int leftchild; int rightchild;
};
struct code
{ int bit[maxn];int start;int weight; char ch;
};
void haffman(int weight[],char text[],int n,haffnode hafftree[])//树的生成
{ int j,m1,m2,x1,x2,i;for(i=0;i< 2*n-1;i++)//初始化哈夫曼树{ if(i < n)//将0到n-1依次储存相应的权重和数字 { hafftree[i].weight=weight[i];hafftree[i].ch=text[i];} else//将到2n-2依次设置权重为0,字符为# { hafftree[i].weight=0; hafftree[i].ch='#'; } hafftree[i].parent=0;//初始化双亲结点 hafftree[i].flag=0;//初始化标记 hafftree[i].leftchild=-1;//初始化左孩子 hafftree[i].rightchild=-1;//初始化右孩子 } for(i=0;i< n-1;i++) { m1=m2=maxvalue;x1=x2=0; for(j=0;j< n+i;j++)//循环找出未最小权重的2个结点,x1储存最小结点的序号,x2储存第2小结点的序号,m1储存最小结点的权重,m2储存第2小结点的权重 { if(hafftree[j].weight< m1&&hafftree[j].flag==0)//如果该结点的权重小于m1,并且未标记使用过,则将其权重以及序号赋值给m1,x1,原x1变x2,原m1变m2 { m2=m1;x2=x1;m1=hafftree[j].weight;x1=j; } else if(hafftree[j].weight< m2&&hafftree[j].flag==0)//如果该结点的权重小于m2,并且未标记使用过,则将其权重以及序号赋值给m2,x2{ m2=hafftree[j].weight; x2=j;} } hafftree[x1].parent=n+i;//最小结点的双亲赋值 hafftree[x2].parent=n+i;//第2小结点的双亲赋值 hafftree[x1].flag=1; //标记最小以及第二小结点为已经使用过 hafftree[x2].flag=1; hafftree[n+i].weight=hafftree[x1].weight+hafftree[x2].weight;//该结点为刚刚找到的最小结点以及第二小结点的双亲,其权重为左右孩子权重的和 hafftree[n+i].leftchild=x1; hafftree[n+i].rightchild=x2;}
}
void haffmancode(haffnode hafftree[],int n,code haffcode[])//编码   忽略!
{ code cd; int i,j; int child,parent;for( i=0;i< n;i++){ cd.start=n-1; cd.weight=hafftree[i].weight; cd.ch=hafftree[i].ch; child=i; parent=hafftree[child].parent; while(parent!=0){ if(hafftree[parent].leftchild==child) cd.bit[cd.start]=0; else cd.bit[cd.start]=1; cd.start--; child=parent; parent=hafftree[child].parent; } for(j=cd.start+1;j< n;j++) haffcode[i].bit[j]=cd.bit[j];haffcode[i].start=cd.start;haffcode[i].weight=cd.weight; haffcode[i].ch=cd.ch;}
}
void ccode(haffnode hafftree[],int n)//翻译解码
{ char data[100];int t=2*n-2;//t为树根结点的序号 scanf("%s",data);//输入需要翻译的字符串 for(int i=0;data[i]!='\n';i++){if(data[i]=='0') t=hafftree[t].leftchild;//如果为0,则向左孩子搜索 else if(data[i]=='1') t=hafftree[t].rightchild;//如果为1,则向右孩子搜索 if(hafftree[t].leftchild==-1)//如果左孩子为-1,则表示已经到了树的底部,则输出其对应的字符,然后重新从根结点继续寻找下一个字符 {printf("%c",hafftree[t].ch);t=2*n-2;}}}int main( )
{ int n=8; int weight[]={5,29,7,8,14,23,3,11};char text[]={'a','b','c','d','e','f','g','h'}; haffnode myhafftree[maxvalue]; code myhaffcode[maxvalue];haffman(weight,text,n,myhafftree);haffmancode(myhafftree,n,myhaffcode); ccode(myhafftree,n); return 0;
}

西南科技大学OJ题 哈夫曼译码0986相关推荐

  1. 西南科技大学OJ题 邻接矩阵到邻接表1055

    邻接矩阵到邻接表 5000(ms) 10000(kb) 2341 / 5552 假设无向图G采用邻接矩阵存储,编写一个算法输出邻接表. 输入 第一行为一个整数n,表示顶点的个数(顶点编号为0到n-1) ...

  2. 西南科技大学OJ题 求最小生成树(Prim算法)1075

    求最小生成树(Prim算法) 1000(ms) 10000(kb) 2256 / 4495 Tags: 生成树 求出给定无向带权图的最小生成树.图的定点为字符型,权值为不超过100的整形.在提示中已经 ...

  3. 西南科技大学OJ题 顺序表插入操作的实现0943

    顺序表插入操作的实现 建立长度为n的顺序表,在指定的数据元素item之前插入数据元素data.如果指定的数据元素item不存在,则将data插入到顺序表的尾端.(数据类型为整型) 输入 第一行为顺序表 ...

  4. 西南科技大学OJ题 单链表的删除操作的实现0953

    单链表的删除操作的实现 1000(ms) 65535(kb) 2896 / 13622 建立长度为n的单链表,删除第i个结点之前的结点. 输入 第一行为自然数n,表示链式线性表的长度: 第二行为n个自 ...

  5. 西南科技大学OJ题 邻接矩阵存储简单路径1070

    邻接矩阵存储简单路径 5000(ms) 10000(kb) 2197 / 4175 Tags: 邻接矩阵 假设无向图G采用邻接矩阵存储,设计一个算法,输出图G中从顶点u到v的所有简单路径. 输入 简单 ...

  6. 西南科技大学OJ题 above average 0706

    above average 1000(ms) 65535(kb) 1531 / 3045 It is said that 90% of frosh expect to be above average ...

  7. matlab完成信源编码译码,matlab哈夫曼译码

    Huffman 编码的 matlab 实现 一.信源编码介绍 为了...经典的方法还是仙农编码 法.费诺编码法和霍夫曼...信源编码和译码,而是事先规定一个 译码差错率的...... 2013 Vol ...

  8. 九度OJ——1172哈夫曼树

    题目描述: 哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和. 输入: 输入有 ...

  9. 山东科技大学OJ题库 1904 帮小明算算数

    1904 帮小明算算数 Description 小明现在正在学习加法,完成家庭作业后,经常需要借助计算机来验证自己做的答案是否正确.请帮小明写一个程序,辅助他进行验证答案. Input 输入只有一行, ...

最新文章

  1. 服务器管理神器 开源堡垒机 jumpserver 实战教程
  2. ImageNet 存在十万标签错误,你知道吗?
  3. 谷歌论文:使用深度强化学习的芯片布局
  4. 在JBuilder8在使用ANT
  5. 学习vue中遇到的报错,特此记录下来
  6. VMware vSphere Client WIN10安装问题
  7. 怎么利用ffmpeg和AviSynth给在windows下面为flv文件加水印
  8. batchsize大小对训练速度的影响
  9. kotlin读取sd卡里的文件_如何在Kotlin中写入文件?
  10. DBCC命令2:状态查询
  11. configure: error: You requested G729 audio codec but not found...die
  12. Node.js 中文乱码解决
  13. 推荐一个springboot和springcloud系列的博客专家--方志朋
  14. 雪球网股票用户评论爬虫
  15. 将编译器的代码快速转存为图片
  16. 使用 Cobertura 和反射机制提高 Java 单元测试中的代码覆盖率
  17. LC Uniboot相比于常规的LC光纤连接器有什么特点?
  18. 使用网络唤醒功能实现远程开机 —— 定时执行专家
  19. x2检验(chi-square test)/ 卡方检验
  20. nubia,无IMEI码(串号丢失)解救【转】

热门文章

  1. Django的前后端分离以及Rest风格接口开发大全
  2. Centos6.8下ActivityMQ安装
  3. LabVIEW使用两三年感触
  4. 折腾win7:保护眼睛,修改资源管理器背景颜色 xp适用
  5. java编程实现删除一个文件夹_Java实现文件夹删除方式总结详解
  6. 分布式解决方案-全面解密分布式任务调度平台-XXLJob调度中心集群
  7. wangeditor 最大字数_我为什么要做富文本编辑器【wangEditor5个月总结】
  8. STM32f103+protues仿真(一) 点亮led
  9. 晶闸管有很多种,最开始发明的是可控硅整流管
  10. 场内指数基金(ETF)有哪些(附完整名单)