SWUSTOJ #986 哈夫曼译码

相信大家上了数据结构课应该是知道哈夫曼译码是什么
它是通过出现概率(权值)来实现一颗哈夫曼树,出现频率最小的数在最下面,最高的在最上面,压缩了数据。
由此可见,我们要做这道题首先就是建一颗哈夫曼树,然后根据左0右1来查找。
当然上述是标准的做法,但是
能ac的代码就是好代码
由题目我们可以知道题目建立的哈夫曼树,然后我们在纸上画出来,写出每个字母的哈夫曼编码,直接打表。
取巧yyds
糊弄考试专用(bushi
ac代码

#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#define int long long
#define fr(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
bool x=false;
void check(string a){
	if(a=="0001"){
		cout << 'a';
	}
	else if(a=="10"){
		cout << "b";
	}
	else if(a=="1110"){
		cout << "c";
	}
	else if(a=="1111"){
		cout << "d";
	}
	else if(a=="110"){
		cout << "e";
	}
	else if(a=="01"){
		cout << "f";
	}
	else if(a=="0000"){
		cout << "g";
	}
	else if(a=="001"){
		cout << "h";
	}
	else{
		x=true;
	}
}
signed main(){
	string a;
	cin >> a;
	string b;
	fr(i,0,a.length()){
		b+=a[i];
		check(b);
		if(!x){
			b="";
		}
		else{
			x=false;
		}
	}
	
}

完成后,记得自己认认真真的去实现一遍哦

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值