题目描述
通常要求根据给定的编码本对密文进行解码。现已给定相应字符的哈夫曼编码,要求根据编码对密文进行解码。(建立哈夫曼树以及编码、主函数等都已经给出,你只需要填写译码函数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”
使用命名空间标准;
struct haffnode
{
字符 ch;
整型重量;
int 标志;
int parent;
int leftchild;
int rightchild;
};
结构代码
{
int bit[maxn];
整型启动;
整型重量;
字符 ch;
};
void haffman(int weight[],char text[],int n,haffnode hafftree[])
{
整型 j,m1,m2,x1,x2,i;
for(i=0;i< 2*n-1;i++)
{
if(i < n)
{
hafftree[i].weight=weight[i];
哈夫特里[i].
ch=text[i];
}
还
{
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=最大值;
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=哈夫树[j].重量;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[])
{
代码 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;
儿童 = 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--;
子项 = 父项;
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);
返回 0;
}
输入
根据哈夫曼树编码表,针对字符串做好的编码结果。
输出
对一行需要解码的串,进行解码,并输出解码后的结果。
样例输入
000100011011101110样例输出
aabccconst int maxvalue=100; const int maxbit=100; const int maxn=100; #include"iostream" #include"stdio.h" #include"stdlib.h" #include<string.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 temp=n*2-2; char str[maxvalue]; scanf("%s",str); int len=strlen(str); for(int i=0;i<len;i++) { if(str[i]=='0') { temp=hafftree[temp].leftchild; } else if(str[i]=='1') { temp=hafftree[temp].rightchild; } if(hafftree[temp].leftchild==-1||hafftree[temp].rightchild==-1) { printf("%c",hafftree[temp].ch); temp=n*2-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; }
SWUST OJ986: 哈夫曼译码
最新推荐文章于 2023-05-02 12:13:56 发布