swust oj 986
哈夫曼译码
通常要求根据给定的编码本对密文进行解码。现已给定相应字符的哈夫曼编码,要求根据编码对密文进行解码。(建立哈夫曼树以及编码、主函数等都已经给出,你只需要填写译码函数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的更多相关文章
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)
题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...
- [Swust OJ 1026]--Egg pain's hzf
题目链接:http://acm.swust.edu.cn/problem/1026/ Time limit(ms): 3000 Memory limit(kb): 65535 hzf ...
- [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 ...
- [Swust OJ 385]--自动写诗
题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535 Descripti ...
随机推荐
- zabbix监控ssl证书到期时间
监控脚本 cat ssl_check.sh #!/bin/bash # #获取ssl证书的过期时间 #menghao #获取证书的有效时间 time=$(echo | openssl s_client ...
- iOS 仿抖音 视频裁剪
1.最近做短视频拍摄.其中的裁剪界面要做得和抖音的视频裁剪效果一样 需求: 裁剪有一个最大裁剪时间.最小裁剪时间.左右拖动可以实时查看对应的视频画面.拖动进度条也能查看对应的画面 .拖动底部视图也能 ...
- js实现多个小球碰撞
实现思路:小球的移动,是通过改变小球的left和top值来改变,坐标分别为(x,y)当x/y值加到最大,即加到父级的宽度或者高度时,使x值或者y值减小,同理当x值或者y值减到最小时,同样的使x值或者y ...
- Django—跨域请求(jsonp)
同源策略 如果两个页面的协议,端口(如果有指定)和域名都相同,则两个页面具有相同的源. 示例:两个Django demo demo1 url.py url(r'^demo1/',demo1), vie ...
- 敏捷开发相关编辑思想(SOA、DDD、REST、CQRS)
这是第一次写有关编程思想的东西. 1.理解Martin Fowler提出的SOA(面向服务歧义) 2.理解DDD(Domain-Driven Design领域驱动设计): http://blog.cs ...
- Django发HTML邮件
1.settings配置 EMAIL_HOST = 'XXXX' DEFAULT_FROM_EMAIL = '张宁 <zhang.ning@XXX.com>' RECEIVER =['zh ...
- sql父子表结构,常用脚本
在实际运用中经常会创建这样的结构表Category(Id, ParentId, Name),特别是用于树形结构时(菜单树,权限树..),这种表设计自然而然地会用到递归,若是在程序中进行递归(虽然在程序 ...
- Flink RichSourceFunction应用,读关系型数据(mysql)数据写入关系型数据库(mysql)
1. 写在前面 Flink被誉为第四代大数据计算引擎组件,即可以用作基于离线分布式计算,也可以应用于实时计算.Flink的核心是转化为流进行计算.Flink三个核心:Source,Transforma ...
- outlook2013 关闭时最小化到任务栏的完美解决方法
使用 Keep Outlook Running 加载项 文件->选项->加载项 点击最下面的“转到”按钮 *用管理员身份运行Outlook才可以将 Keep Outlook Running ...
- 搭建 eclipse,maven,tomcat 环境
1,安装jdk 2,安装eclipse,可以写java程序 3,安装tomcat,可以写简单的web页面 4,安装maven,再eclipse中可以构建maven管理的java程序 5,将maven程 ...