哈夫曼译码

1000(ms)
10000(kb)
1997 / 4186

通常要求根据给定的编码本对密文进行解码。现已给定相应字符的哈夫曼编码,要求根据编码对密文进行解码。(建立哈夫曼树以及编码、主函数等都已经给出,你只需要填写译码函数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 
 const int maxvalue=;
const int maxbit=;
const int maxn=;
#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=; i< *n-; i++)
{
if(i < n)
{
hafftree[i].weight=weight[i];
hafftree[i].
ch=text[i];
}
else
{
hafftree[i].weight=;
hafftree[i].ch='#';
}
hafftree[i].parent=;
hafftree[i].flag=;
hafftree[i].leftchild=-;
hafftree[i].rightchild=-;
}
for(i=; i< n-; i++)
{
m1=m2=maxvalue;
x1=x2=;
for(j=; j< n+i; j++)
{
if(hafftree[j].weight< m1&&hafftree[j].flag==)
{
m2=m1;
x2=x1;
m1=hafftree[j].weight;
x1=j;
}
else if(hafftree[j].weight< m2&&hafftree[j].flag==)
{
m2=hafftree[j].weight;
x2=j;
}
}
hafftree[x1].parent=n+i;
hafftree[x2].parent=n+i;
hafftree[x1].flag=;
hafftree[x2].flag=;
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=; i< n; i++)
{
cd.start=n-;
cd.weight=hafftree[i].weight;
cd.ch=hafftree[i].ch;
child=i;
parent=hafftree[child].parent;
while(parent!=)
{
if(hafftree[parent].leftchild==child)
cd.bit[cd.start]=;
else
cd.bit[cd.start]=;
cd.start--;
child=parent;
parent=hafftree[child].parent;
}
for(j=cd.start+; 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 i,j=,m=*n-;
char b[maxn];
memset(b,'\0',sizeof(b));
i=m-;
scanf("%s",b);
while(b[j]!='\0')
{
if(b[j]=='')
i=hafftree[i].leftchild;
else
i=hafftree[i].rightchild;
if(hafftree[i].leftchild==-)
{
printf("%c",hafftree[i].ch);
i=m-;
}
j++;
}
}
int main( )
{
int n=;
int weight[]= {,,,,,,,};
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 ;
}

swust oj 986的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  5. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  6. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  7. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  8. [Swust OJ 1139]--Coin-row problem

    题目链接:  http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...

  9. [Swust OJ 385]--自动写诗

    题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535    Descripti ...

随机推荐

  1. Asp.net Mvc 与WebForm 混合开发

      根据项目实际需求,有时候会想在项目中实现Asp.net Mvc与Webform 混合开发,比如前台框架用MVC,后台框架用WebForm.其实要是实现也很简单,如下: (1)在MVC 中使用Web ...

  2. dos.orm的事务处理

    dos.orm也包含事务处理,没有太多封装,这里有几个简单的示例代码. using (DbTrans trans = DbSession.Default.BeginTransaction()) { D ...

  3. 题解 P4093 【[HEOI2016/TJOI2016]序列】

    这道题原来很水的? noteskey 一开始以为是顺序的 m 个修改,然后选出一段最长子序列使得每次修改后都满足不降 这 TM 根本不可做啊! 于是就去看题解了,然后看到转移要满足的条件的我发出了黑人 ...

  4. springboo+nginx测试反向代理02

    本节对nginx配置方面会略微研究~~ 1:切换到 /opt/nginx-1.8.1/conf 目录,将nginx.conf文件拷贝到 /myprojects/nginx 目录下 2:切换到/opt/ ...

  5. db2数据库备份与恢复

    备份 先停掉Tomcat,然后在机器A上执行以下语句: db2stop force db2start db2 force application all db2 backup database pos ...

  6. EndNote中文文献导入出错和数量限制解决

    发现之前记录的存在忽略,把存在的一个重要问题遗漏了,Endnote中文文献导入无法导入,软件奔溃问题,现在在原先基础上补上(补到最后): ..一路绿色φ(>ω<*) φ(>ω< ...

  7. Bootstrap-datepicker3官方文档中文翻译---I18N/国际化(原文链接 http://bootstrap-datepicker.readthedocs.io/en/latest/index.html)

    I18N/国际化 这个插件支持月份和星期名以及weekStart选项的国际化.默认是英语(“en”); 其他有效的译本语言在 js/locales/ 目录中, 只需在插件后包含您想要的地区. 想要添加 ...

  8. Python深度学习(Deep Learning with Python) 中文版+英文版+源代码

    Keras作者.谷歌大脑François Chollet最新撰写的深度学习Python教程实战书籍(2017年12月出版)介绍深入学习使用Python语言和强大Keras库,详实新颖.PDF高清中文版 ...

  9. UOJ#375. 【ZJOI2018】迷宫

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ375.html 题解 首先,我们可以建出一个 k 个点的自动机,第 i 个点表示当前数对 k 取模为 i- ...

  10. Python3-Cookbook总结 - 第二章:字符串和文本

    第二章:字符串和文本 几乎所有有用的程序都会涉及到某些文本处理,不管是解析数据还是产生输出. 这一章将重点关注文本的操作处理,比如提取字符串,搜索,替换以及解析等. 大部分的问题都能简单的调用字符串的 ...