hdu 1053 Entropy

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1053  

Entropy

Description

An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with “wasted” or “extra” information removed. In other words, entropy encoding removes information that was not necessary in the first place to accurately encode the message. A high degree of entropy implies a message with a great deal of wasted information; english text encoded in ASCII is an example of a message type that has very high entropy. Already compressed messages, such as JPEG graphics or ZIP archives, have very little entropy and do not benefit from further attempts at entropy encoding.

English text encoded in ASCII has a high degree of entropy because all characters are encoded using the same number of bits, eight. It is a known fact that the letters E, L, N, R, S and T occur at a considerably higher frequency than do most other letters in english text. If a way could be found to encode just these letters with four bits, then the new encoding would be smaller, would contain all the original information, and would have less entropy. ASCII uses a fixed number of bits for a reason, however: it’s easy, since one is always dealing with a fixed number of bits to represent each possible glyph or character. How would an encoding scheme that used four bits for the above letters be able to distinguish between the four-bit codes and eight-bit codes? This seemingly difficult problem is solved using what is known as a “prefix-free variable-length” encoding.

In such an encoding, any number of bits can be used to represent any glyph, and glyphs not present in the message are simply not encoded. However, in order to be able to recover the information, no bit pattern that encodes a glyph is allowed to be the prefix of any other encoding bit pattern. This allows the encoded bitstream to be read bit by bit, and whenever a set of bits is encountered that represents a glyph, that glyph can be decoded. If the prefix-free constraint was not enforced, then such a decoding would be impossible.

Consider the text “AAAAABCD”. Using ASCII, encoding this would require 64 bits. If, instead, we encode “A” with the bit pattern “00”, “B” with “01”, “C” with “10”, and “D” with “11” then we can encode this text in only 16 bits; the resulting bit pattern would be “0000000000011011”. This is still a fixed-length encoding, however; we’re using two bits per glyph instead of eight. Since the glyph “A” occurs with greater frequency, could we do better by encoding it with fewer bits? In fact we can, but in order to maintain a prefix-free encoding, some of the other bit patterns will become longer than two bits. An optimal encoding is to encode “A” with “0”, “B” with “10”, “C” with “110”, and “D” with “111”. (This is clearly not the only optimal encoding, as it is obvious that the encodings for B, C and D could be interchanged freely for any given encoding without increasing the size of the final encoded message.) Using this encoding, the message encodes in only 13 bits to “0000010110111”, a compression ratio of 4.9 to 1 (that is, each bit in the final encoded message represents as much information as did 4.9 bits in the original encoding). Read through this bit pattern from left to right and you’ll see that the prefix-free encoding makes it simple to decode this into the original text even though the codes have varying bit lengths.

As a second example, consider the text “THE CAT IN THE HAT”. In this text, the letter “T” and the space character both occur with the highest frequency, so they will clearly have the shortest encoding bit patterns in an optimal encoding. The letters “C”, “I’ and “N” only occur once, however, so they will have the longest codes.

There are many possible sets of prefix-free variable-length bit patterns that would yield the optimal encoding, that is, that would allow the text to be encoded in the fewest number of bits. One such optimal encoding is to encode spaces with “00”, “A” with “100”, “C” with “1110”, “E” with “1111”, “H” with “110”, “I” with “1010”, “N” with “1011” and “T” with “01”. The optimal encoding therefore requires only 51 bits compared to the 144 that would be necessary to encode the message with 8-bit ASCII encoding, a compression ratio of 2.8 to 1.

Input

The input file will contain a list of text strings, one per line. The text strings will consist only of uppercase alphanumeric characters and underscores (which are used in place of spaces). The end of the input will be signalled by a line containing only the word “END” as the text string. This line should not be processed.

Output

For each text string in the input, output the length in bits of the 8-bit ASCII encoding, the length in bits of an optimal prefix-free variable-length encoding, and the compression ratio accurate to one decimal point.

Sample Input

AAAAABCD
THE_CAT_IN_THE_HAT
END

Sample Output

64 13 4.9
144 51 2.8

哈弗曼编码。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<set>
using std::set;
using std::sort;
using std::swap;
using std::string;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 30;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
char buf[11000];
struct Node {
	char dat;
	int w;
	Node *ch[2];
	Node(char _dat_, int _w_, Node *l = NULL, Node *r = NULL) {
		dat = _dat_, w = _w_;
		ch[0] = l, ch[1] = r;
	}
	Node(const Node &x) {
		dat = x.dat, w = x.w;
		ch[0] = x.ch[0], ch[1] = x.ch[1];
	}
	inline bool operator<(const Node &x) const {
		return w > x.w;
	}
};
struct Hoffmancode {
	int sum;
	Node *root;
	priority_queue<Node> q;
	inline char to_lower(char ch) {
			return ch >= 'A' && ch <= 'Z' ? ch + 32 : ch;
	}
	inline void CreateHoffmanTree() {
		Node *l = NULL, *r = NULL;
		while (!q.empty() && q.size() != 1) {
			l = new Node(q.top()); q.pop();
			r = new Node(q.top()); q.pop();
			Node ret(0, l->w + r->w, l, r);
			q.push(ret);
		}
		if (!q.empty()) {
			root = new Node(q.top()); q.pop();
		}
	}
	inline void CreateHoffmanCode(Node *x, string str) {
		if (!x) return;
		if (x->dat != 0) {
			sum += (str.length() * x->w);
		}
		CreateHoffmanCode(x->ch[0], str + "0");
		CreateHoffmanCode(x->ch[1], str + "1");
	}
	inline void solve() {
		sum = 0, root = NULL;
		int arr[N] = { 0 }, tot = 0, n = strlen(buf);
		while (!q.empty()) q.pop();
		for (int i = 0; i < n; i++) {
			arr[to_lower(buf[i]) - '_']++;
		}
		for (int i = 0; i < 28; i++) {
			if (arr[i]) {
				tot++;
				Node ret('_' + i, arr[i]);
				q.push(ret);
			}
		}
		if (1 == tot) {
			printf("%d %d 8.0\n", n * 8, n);
			return;
		}
		CreateHoffmanTree();
		CreateHoffmanCode(root, "");
		printf("%d %d %.1lf\n", n * 8, sum, (double)(n * 8) / sum);
	}
}work;
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	while (~scanf("%s", buf) && 0 != strcmp(buf, "END")) {
		work.solve();
	}
	return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4865026.html

深入剖析 Wenet 下 AISHELL-1 的 run.sh 脚本:从数据准备到模型训练 run.sh脚本用于处理 AISHELL-1 数据集的整个训练和测试流程。我们将逐步分析脚本中的每个部分,包括环境变量、参数设置、各个阶段的执行等。stage: 设置脚本的起始阶段(如数据准备)。stop_stage: 设置脚本的结束阶段。通过对 Wenet 下 AISHELL-1 的run.sh脚本的详细剖析,我们希望你能更好地理解每个步骤的功能及其背后的原理。此脚本不仅为模型训练提供了便利,也为用户自定义和扩展训练流程提供了基础。希望本教程能帮助你顺利完成语音识别项目! 阅读详情

相关推荐

Android GNSS 模块分析(三)JNI 层

Android GNSS;定位服务;GPS;NMEA协议;LocationManagerService;导航;

Yang_Mao_Shan的博客 5161

哈夫曼编码压缩比问题:POJ1521 熵

题目描述: An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with "wasted" or "extra" information removed. In other words, entropy encoding removes information that was not necessary in the first place t

qq_48988844的博客 824

单细胞测序避坑指南:为什么你的免疫组库数据总出问题?5‘转录组7个关键细节

本文深度解析了5'端单细胞测序技术在免疫组库研究中的应用与常见问题。文章从5'端建库的分子机制入手,重点剖析了其在捕获TCR/BCR序列上的独特优势,并系统梳理了从样本处理、文库构建到数据质控全流程中7个最易出错的细节,为研究者提供了一份实用的避坑指南,旨在帮助提升免疫组库数据的质量和可靠性。

weixin_29208327的博客 482

POJ - 1521 Entropy(哈夫曼编码)

题目链接:https://vjudge.net/contest/361484#problem/J An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with “wasted” or “extra” information removed...

zaiyang遇见 615

用Quartus II和LPM_ROM手把手复现西电计组课设:从微指令到五条指令的完整流程

本文详细介绍了使用Quartus II和LPM_ROM复现西电计组课设的完整流程,从微指令编写到五条指令的实现。通过环境搭建、LPM_ROM配置、微程序设计及调试技巧,帮助读者高效完成课程设计,避开常见问题。适合计组课程设计的学习者和实践者参考。

weixin_29288313的博客 457

CTR模型:Deep&Cross Network

1. 简介 DCN模型(Deep & Cross Network for Ad Click Predictions)是2017年提出的。 出发点: (1)人工探索所有的交叉特征是不现实的; (2)难于推广未曾出现过的交叉特征。 DNN能够自动探索交叉特征并推广到未出现过的交叉特征,但是DNN的学习效率不高,且无法显式的学习特征交叉。 Deep&Cross Network, DCN 模型保留了DNN的优点,并提出了新的cross network,高效学习高阶特征交互。 优点: (1)cross

rosefun96的博客 9361

霍夫曼树的应用

Entropy Time Limit:1000MS Memory Limit:65536K Total Submit:23 Accepted:13 Description An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message wit

Wei_Yuan_2012的专栏 1255

使用 MATLAB/Simulink 构建一个 光伏-储能联合系统接入10kV配电网 的协同控制仿真模型

本教程详细介绍了基于Simulink的光伏-储能联合系统在配电网中的协同控制仿真方法。主要内容包括:系统结构设计(光伏阵列、储能电池、双向变流器等);建模流程(MPPT控制、BMS系统、PCS矢量控制);核心协同控制策略(功率平滑、削峰填谷、电压支撑的三层控制架构);仿真结果分析方法及性能评估指标。教程提供了从组件建模到系统集成的完整指导,并建议了模型预测控制、微网孤岛运行等进阶方向,帮助开发者掌握智能电网的关键仿真技术。通过该模型可实现可再生能源的高效利用与电网稳定运行的平衡。

amy_mhd的博客 217

Entropy (p1521)

像这样的题的思路,我是很难想到的。只好看人家的,不好,不好。 #include #include #include #include #include #include #include #include #include #include #include using namespace std; priority_queue,greater> q; int len[1000];

jiangjiashi的博客 867

智能车图像处理0-图像处理整体框架思路

1.总体框架如上所示,具体代码请查看后续文章。2.因为参与竞赛时间是大二,所以在编程中有许多不规范的地方(如变量的命名),大家见谅。3.原创文章,请勿侵权=-=

taiyuezyh的博客 1万+

《模型轻量化-剪枝蒸馏量化系列》YOLOv5无损剪枝(附源码)

无损剪枝模型到几百kb~

cv君的博客 3万+

贪心算法之Entropy

Entropy Description An entropy encoderis a data encoding method that achieves lossless data compression by encoding amessage with "wasted" or "extra" information removed. Inother words, entropy enco

hqf1992的专栏 721

注意力机制篇 | YOLOv8改进之在C2f模块添加EMA注意力机制(附2种改进方法)

EMA(Exponential Moving Average)注意力机制是一种用于增强模型性能的注意力机制,它通过对模型的特征图进行加权平均来提取更有用的特征信息。具体来说,EMA注意力机制通过引入一个权重因子来调整特征图中每个位置的重要性,从而使模型能够更好地关注重要的特征。🌈

小哥谈 4180

hdu 1053Entropy

hdu 1053Entropy 给定一个字符串,计算hu

gwq5210的专栏 853

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Hdu 1053 Entropy

大意:裸地哈弗曼编码,而且不要求输出编码,只要求输出最短长度。 思路:可以与Hdu 2527 用一样的方法水过,因为这里不需要求编码,但我写这一题主要是为了学习哈弗曼建树,所以也用建树的方法写了一遍。建好树后,我们可以通过DFS遍历树,也可以通过BFS遍历树。 CODE: #include #include #include #include using namespace st

Wall_F的专栏 909

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

HDU1053 Entropy

Entropy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6285    Accepted Submission(s): 2647 Problem Description An entropy encoder

BorntoDo的博客 423

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

HDU 1053 Entropy

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1053 题目意思:给定一个字符串,这个字符串只由大写字母和下划线组成,在计算机中,每个字母占8个字节,如果不使用编码,所需的空间是多少字节?如果使用编码,那么所需的空间是多少字节。最后输出不使用编码所占的空间,使用编码所占的空间,以及它们的比值。 不使用编码的话,那么很显然,所占空间=字符串的长

哇哈哈的专栏 621

HDU1053——Entropy,HDU1054——Strategic Game,HDU1055——Color a Tree

如果我们改为用位模式“00”编码“A”,“01”编码“B”,“10”编码“C”,“11”编码“D”,那么我们可以仅用 16 位来编码此文本;一个最优编码是用“0”编码“A”,“10”编码“B”,“110”编码“C”,“111”编码“D”。一种这样的最优编码是用“00”编码空格,“100”编码“A”,“1110”编码“C”,“1111”编码“E”,“110”编码“H”,“1010”编码“I”,“1011”编码“N”和“01”编码“T”。然而,字母“C”、“I”和“N”只出现一次,所以它们将具有最长的代码。

u014114223的博客 1151

杭电hdu 1053 Entropy题解

http://acm.hdu.edu.cn/showproblem.php?pid=1053 这题就是运用哈夫曼的编码思想,然后统计用哈夫曼编码后的二进制长度,以及运用ascii编码长度的比较。我在这题上感觉统计无从下手,虽然知道哈夫曼的贪心实现,但真正的到运用的时候了,却一时想不起应该怎么用代码去实现,进过参考别人用优先队列写的代码,我也写出了我自己的代码。现记录在下面。做个在编程上的参考。

凌语小辉 2578
上一篇: hdu 5444 Elven Postman
下一篇: poj 2777 Count Color
dong12276
博客等级 码龄13年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值