简介:

通过统计一篇文章(或一本书)中每个字符出现的频率(比如字母a出现了100次),对文中出现的每个字符进行编码。

这种编码的特点是出现频率越高的字符,其编码长度越短。

有了这样的字符、编码对照表,就可以发“密文”啦!

举个栗子:

早上看到一篇新闻,blablabla内容不重要。如下:

Apple Inc. plans to break with its recent pattern of overhauling the design of its flagship iPhone every two years, and make only subtle changes in the models it will release this fall, according to people familiar with the matter.
Among other things, those people said, Apple’s newest phones will maintain the current 4.7-inch and 5.5-inch displays. Those are the sizes of the larger-display models Apple introduced in 2014, and refreshed with newer models last fall.
The biggest planned change in this year’s phones is the removal of the headphone plug, which will make the phone thinner and improve its water resistance, said people with that matter.
The Lightning connector will serve double-duty as a port for charging the phone and for connecting headphones, they said. KGI Securities analyst Ming-Chi Kuo said he expects the new iPhone to be one millimeter thinner than the current iPhone.
Apple plans bigger design changes for 2017, the 10th anniversary of the original iPhone. Those changes could include an edge-to-edge organic light-emitting diode, or OLED, screen and eliminating the home button by building the fingerprint sensor into the display, according to people familiar with the matter.
An Apple spokeswoman declined to comment.
In the past, Apple has introduced new iPhones on a “ticktock” cycle. Apple delivers major design changes every other year—the “tick” years—followed by software improvements and hardware refinements in the “tock” years.
It isn’t clear whether this year’s shift is a temporary or permanent departure from this pattern. Either way, it comes at a precarious moment for the iPhone, Apple’s most important product, accounting for two-thirds of the company’s revenue.
Apple is dealing with the first decline in iPhone sales in the product’s history as demand slows and once-torrid markets such as China cool.
Advertisement
The introduction of larger-display phones in 2014 triggered a surge in sales, with iPhone sales rising 37% in the fiscal year ending September 2015. In the prior year, when Apple introduced the iPhone 5S with a near identical design to the iPhone 5, iPhone sales increased 13%.
At a meeting with an Apple executive last month, one of the company’s China-based engineers asked why this year’s model lacked a major design change in keeping with Apple’s usual two-year cycle. The answer, one person at the meeting recalled, was that the new technology in the pipeline will take time to implement.
Apple Coverage
China Bites Apple
Apple Outlines Siri Improvements
Dear Apple, Please Make the iPhone Smarter
Siri: Once a Flake, Now Key to Apple’s Future
People familiar with the matter said some features that Apple hopes to integrate into iPhones, such as curved screens, weren’t ready for this year’s models.
Previous big design changes to the phone helped create buzz around the new models and spur sales by enticing existing iPhone customers to upgrade. Apple’s latest phones—the iPhone 6S and 6S Plus—haven’t generated as much excitement, in part because they closely resemble the previous year’s models.
In an interview on CNBC last month, Apple Chief Executive Tim Cook said that there is “great innovation” in the pipeline for future iPhones that will drive customers to upgrade. “We are going to give you things that you can’t live without that you just don’t know you need today,” Mr. Cook said.
In a note to clients last week, Credit Suisse analyst Kulbinder Garcha said he expects “muted” sales increases for the forthcoming models because he expects them to be a “modest upgrade” to the existing phones. Mr. Garcha said he expects a “super cycle” for the following year.
Horace Dediu, founder of Asymco, an independent analysis firm focused on the mobile industry, said many consumers buy new phones when they are due for an upgrade under the terms of their wireless plan, without much concern for new features or design changes. While other companies try to entice consumers to upgrade phones with new features, he said Apple “marches to the beat of a different drummer.”
Apple has delivered meaningful improvements to the iPhone even in years when it didn’t change the phone’s look. For example, it added a fingerprint sensor to the iPhone 5S, but that didn’t alter the look of the device.
The models introduced last year, while the same size as their predecessors, included faster processors, new touch-screen features and better cameras. Apple tried to capture the changes with its marketing slogan for iPhone 6S and 6S Plus: “The only thing that’s changed is everything.”
For years, Apple Chief Design Officer Jony Ive has expressed a desire for the iPhone to appear like a single sheet of glass, according to people familiar with the matter. The current design ideas for the 2017 iPhones are expected to push the handsets in that direction by eliminating much of the bezel around the display with the OLED screen.
Apple hasn’t completed the designs for next year’s phones, so it is possible that these ideas may not be implemented, they said.
Write to Daisuke Wakabayashi at Daisuke.Wakabayashi@wsj.com and Eva Dou at eva.dou@wsj.com

通过统计字符频率并编码后,得到对照表:

字符: 频率: 822 编码: 110
字符: % 频率: 2 编码: 00101100101
字符: ’ 频率: 3 编码: 11100011100
字符: , 频率: 44 编码: 1110000
字符: - 频率: 14 编码: 111011101
字符: . 频率: 43 编码: 1010101
字符: 0 频率: 6 编码: 1110001100
字符: 1 频率: 7 编码: 001000000
字符: 2 频率: 5 编码: 0010111110
字符: 3 频率: 2 编码: 00101100110
字符: 4 频率: 3 编码: 11100011101
字符: 5 频率: 6 编码: 1110001101
字符: 6 频率: 4 编码: 0010000100
字符: 7 频率: 4 编码: 0010000101
字符: : 频率: 2 编码: 00101100111
字符: @ 频率: 2 编码: 00101101000
字符: A 频率: 30 编码: 0000010
字符: B 频率: 2 编码: 00101101001
字符: C 频率: 12 编码: 111000100
字符: D 频率: 8 编码: 001000001
字符: E 频率: 5 编码: 0010111111
字符: F 频率: 4 编码: 0010000110
字符: G 频率: 3 编码: 11100011110
字符: H 频率: 1 编码: 001011010110
字符: I 频率: 9 编码: 001011011
字符: J 频率: 1 编码: 001011010111
字符: K 频率: 4 编码: 0010000111
字符: L 频率: 3 编码: 11100011111
字符: M 频率: 4 编码: 0010110000
字符: N 频率: 2 编码: 00101101010
字符: O 频率: 5 编码: 1010100000
字符: P 频率: 26 编码: 11101100
字符: S 频率: 12 编码: 111000101
字符: T 频率: 10 编码: 001011100
字符: W 频率: 5 编码: 1010100001
字符: a 频率: 277 编码: 0100
字符: b 频率: 31 编码: 0000011
字符: c 频率: 124 编码: 00001
字符: d 频率: 150 编码: 10011
字符: e 频率: 583 编码: 011
字符: f 频率: 64 编码: 001001
字符: g 频率: 90 编码: 101011
字符: h 频率: 233 编码: 11110
字符: i 频率: 292 编码: 1000
字符: j 频率: 5 编码: 1010100010
字符: k 频率: 27 编码: 11101101
字符: l 频率: 159 编码: 10100
字符: m 频率: 99 编码: 111010
字符: n 频率: 291 编码: 0101
字符: o 频率: 255 编码: 0001
字符: p 频率: 144 编码: 10010
字符: r 频率: 235 编码: 11111
字符: s 频率: 270 编码: 0011
字符: t 频率: 344 编码: 1011
字符: u 频率: 93 编码: 111001
字符: v 频率: 32 编码: 0010001
字符: w 频率: 61 编码: 000000
字符: x 频率: 13 编码: 111011100
字符: y 频率: 67 编码: 001010
字符: z 频率: 5 编码: 1010100011
字符: — 频率: 4 编码: 0010110001
字符: ’ 频率: 21 编码: 10101001
字符: “ 频率: 10 编码: 001011101
字符: ” 频率: 10 编码: 001011110

那么请问,下面这串“密文”是什么意思呢?

1110001000000111010010100000111010101000010001111111010010011

直接上代码。请用python3解释器

class Node(object):def __init__(self, freq):self.freq = freqself.left = Noneself.right = Noneself.father = Nonedef isLeft(self):return self.father.left == selfdef createHuffmanTree(C):n = len(C)Q = C[:]for i in range(n-1):Q.sort(key=lambda x: x.freq)x = Q.pop(0)y = Q.pop(0)node = Node(x.freq + y.freq)node.left = xnode.right = yx.father = nodey.father = nodeQ.append(node)Q[0].father = Nonereturn Q[0]def preTraverse(root):  if root==None:  return  print(root.freq)  preTraverse(root.left)  preTraverse(root.right)def huffmanEncoding(C, root):codeList = [''] * len(C)for i in range(len(C)):tmpNode = C[i]while tmpNode != root:if tmpNode.isLeft():codeList[i] = "0" + codeList[i]else:codeList[i] = "1"+ codeList[i]tmpNode = tmpNode.fatherreturn codeListdef huffmanDecode(code, codingDict):res = ""decodeDict = {value: key for key, value in codingDict.items()}tmpCode = ""while code:tmpCode += code[0]code = code[1:]ch = decodeDict.get(tmpCode, None)if ch:res = res + chtmpCode = ""return resdef wordCount(strText):chDict = {}for ch in strText:if chDict.get(ch, None):chDict[ch] += 1else:chDict[ch] = 1return list(map(lambda x: (x[0], x[1]), chDict.items()))

于是,解决方法就是,把上面那篇文章作为输入,得到字符编码对照表后调用解码程序就好啦!

strText = """Apple Inc. plans to break with its recent pattern of overhauling the design of its flagship iPhone every two years, and make only subtle changes in the models it will release this fall, according to people familiar with the matter.
Among other things, those people said, Apple’s newest phones will maintain the current 4.7-inch and 5.5-inch displays. Those are the sizes of the larger-display models Apple introduced in 2014, and refreshed with newer models last fall.
The biggest planned change in this year’s phones is the removal of the headphone plug, which will make the phone thinner and improve its water resistance, said people with that matter.
The Lightning connector will serve double-duty as a port for charging the phone and for connecting headphones, they said. KGI Securities analyst Ming-Chi Kuo said he expects the new iPhone to be one millimeter thinner than the current iPhone.
Apple plans bigger design changes for 2017, the 10th anniversary of the original iPhone. Those changes could include an edge-to-edge organic light-emitting diode, or OLED, screen and eliminating the home button by building the fingerprint sensor into the display, according to people familiar with the matter.
An Apple spokeswoman declined to comment.
In the past, Apple has introduced new iPhones on a “ticktock” cycle. Apple delivers major design changes every other year—the “tick” years—followed by software improvements and hardware refinements in the “tock” years.
It isn’t clear whether this year’s shift is a temporary or permanent departure from this pattern. Either way, it comes at a precarious moment for the iPhone, Apple’s most important product, accounting for two-thirds of the company’s revenue.
Apple is dealing with the first decline in iPhone sales in the product’s history as demand slows and once-torrid markets such as China cool.
Advertisement
The introduction of larger-display phones in 2014 triggered a surge in sales, with iPhone sales rising 37% in the fiscal year ending September 2015. In the prior year, when Apple introduced the iPhone 5S with a near identical design to the iPhone 5, iPhone sales increased 13%.
At a meeting with an Apple executive last month, one of the company’s China-based engineers asked why this year’s model lacked a major design change in keeping with Apple’s usual two-year cycle. The answer, one person at the meeting recalled, was that the new technology in the pipeline will take time to implement.
Apple CoverageChina Bites AppleApple Outlines Siri ImprovementsDear Apple, Please Make the iPhone SmarterSiri: Once a Flake, Now Key to Apple’s Future
People familiar with the matter said some features that Apple hopes to integrate into iPhones, such as curved screens, weren't ready for this year’s models.
Previous big design changes to the phone helped create buzz around the new models and spur sales by enticing existing iPhone customers to upgrade. Apple’s latest phones—the iPhone 6S and 6S Plus—haven't generated as much excitement, in part because they closely resemble the previous year’s models.
In an interview on CNBC last month, Apple Chief Executive Tim Cook said that there is “great innovation” in the pipeline for future iPhones that will drive customers to upgrade. “We are going to give you things that you can’t live without that you just don’t know you need today,” Mr. Cook said.
In a note to clients last week, Credit Suisse analyst Kulbinder Garcha said he expects “muted” sales increases for the forthcoming models because he expects them to be a “modest upgrade” to the existing phones. Mr. Garcha said he expects a “super cycle” for the following year.
Horace Dediu, founder of Asymco, an independent analysis firm focused on the mobile industry, said many consumers buy new phones when they are due for an upgrade under the terms of their wireless plan, without much concern for new features or design changes. While other companies try to entice consumers to upgrade phones with new features, he said Apple “marches to the beat of a different drummer.”
Apple has delivered meaningful improvements to the iPhone even in years when it didn’t change the phone’s look. For example, it added a fingerprint sensor to the iPhone 5S, but that didn’t alter the look of the device.
The models introduced last year, while the same size as their predecessors, included faster processors, new touch-screen features and better cameras. Apple tried to capture the changes with its marketing slogan for iPhone 6S and 6S Plus: “The only thing that’s changed is everything.”
For years, Apple Chief Design Officer Jony Ive has expressed a desire for the iPhone to appear like a single sheet of glass, according to people familiar with the matter. The current design ideas for the 2017 iPhones are expected to push the handsets in that direction by eliminating much of the bezel around the display with the OLED screen.
Apple hasn't completed the designs for next year’s phones, so it is possible that these ideas may not be implemented, they said.
Write to Daisuke Wakabayashi at Daisuke.Wakabayashi@wsj.com and Eva Dou at eva.dou@wsj.com """chList = wordCount(strText)
chList.sort(key=lambda x: x[0])
# print(chList)C = [Node(x[1]) for x in chList]
root = createHuffmanTree(C)# 验证生成的树与设计一致
# preTraverse(root)codeList = huffmanEncoding(C, root)
codingDict = {}
for ch, code in zip(chList, codeList):codingDict[ch[0]] = codeprint("字符: ", ch[0],"频率: ", ch[1], "编码: ", code)code = "1110001000000111010010100000111010101000010001111111010010011"
res = huffmanDecode(code, codingDict)
print("decode: ", code, "=>", res)

哦,原来就是Hello World!!!!!!

加密与压缩,霍夫曼编码解码相关推荐

  1. 数据结构与算法之霍夫曼编码解码实现

    目标:将字符串"can you can a can as a can canner can a can."编码再解码 流程: 将字符串转成bytes (byte[]格式)(eg.[ ...

  2. 创建霍夫曼树,霍夫曼编码以及使用霍夫曼编码压缩文件

    那么,什么是霍夫曼树(赫夫曼树)呢? 给定n个权值(权值就是每个节点里面存放的数据,但是根据业务需求不同,存放的数据类型有些差别)作为n个叶子结点,构造一棵二叉树,若该树的带权路径长度达到最小,称这样 ...

  3. 数据结构与算法 / 霍夫曼树、霍夫曼编码和解码

    一. 诞生原因 找出存放一串字符所需的最少的二进制编码. 二. 构造方法 首先统计出每种字符出现的频率,即:概率.权值. 例如:频率表 A:60,    B:45,   C:13   D:69   E ...

  4. Python实现英文文本的霍夫曼编码压缩

    霍夫曼编码作为变长码,在已知字符出现的频率的前提下,将频率高的字符用短码表示,频率低的字符用长码表示,实现用最短的码符号完整的表示出一段文本的信息. 例如 对于 这样一文本,进行字符频率统计,部分结果 ...

  5. 【C语言->数据结构与算法】->哈夫曼压缩解压缩->第一阶段->哈夫曼编码解码的实现

    文章目录 Ⅰ 前言 Ⅱ 代码实现哈夫曼编码&解码 A. 构造哈夫曼树 a. 频度统计 b. 生成哈夫曼树 ① 初始化节点 ② 查找频度最小节点 ③ 哈夫曼树的构造 B. 哈夫曼编码 a. 得到 ...

  6. 霍夫曼编码的三个缺点

    霍夫曼编码是理论上最优的压缩编码,适用于压缩,并不适用于字符编码.将unicode中的100多万个字符统一使用霍夫曼编码,是个不成熟的设想.具体分析,它有三个缺点. 一.对起点敏感.需要从文本的开头处 ...

  7. 【数据结构】图解霍夫曼编码,看了就能懂

    今天来给大家普及一下霍夫曼编码(Huffman Coding),一种用于无损数据压缩的熵编码算法,由美国计算机科学家大卫·霍夫曼在 1952 年提出--这么专业的解释,不用问,来自维基百科了. 说实话 ...

  8. 哈夫曼编码压缩率计算_程序员的算法课(8)-贪心算法:理解霍夫曼编码

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/ ...

  9. 程序员的算法课(8)-贪心算法:理解霍夫曼编码

    一.一种很贪婪的算法定义 贪心是人类自带的能力,贪心算法是在贪心决策上进行统筹规划的统称. [百度百科]贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体 ...

最新文章

  1. 引用http开头的JS失败以及laravel的url()方法的坑
  2. docker安装软件(vim,service)
  3. 【SpringMVC笔记】拦截器 + 文件上传下载
  4. Zmodem协议工作原理
  5. 支付宝H5支付,ISV权限不足
  6. win10+tensorflow1.14+cuda10安装踩坑
  7. 巧用 Automator,为 Mac 创建自定义右键菜单
  8. 微信企业消息推送方案
  9. 使用C#开发Excel插件
  10. Android 热补丁动态修复
  11. 发那科sub_FANUC PMC功能指令之计数器 CTR SUB5-共享屋共享屋
  12. python 仪表盘 ppt_Python之pyecharts数据可视化,词云图,仪表盘!
  13. 个人网站必备的 10 个开源后台管理UI库
  14. linux ln命令
  15. python爬取网页返回响应中出现百度安全验证的解决办法
  16. 喝可乐问题,2个空瓶换一瓶可乐
  17. solidworks装配体改为柔性_Y型钣金通风管道用SolidWorks怎么建模?溪风实战讲解
  18. 《荀子·劝学篇》原文 翻译
  19. 金属学与热处理复习总结
  20. Axure RP8原型设计(一)

热门文章

  1. HPC帮助工程师在大约两小时内模拟飞机的起飞
  2. Pick-up sticks POJ - 2653
  3. 【程序员修炼之道】你的知识资产
  4. [转载]Dalvik指令集
  5. 计算机应用设计大赛获奖信息,北京大学第六届“计算机应用设计大赛”圆满落幕...
  6. android后台进程常驻,android 后台常驻,不会被kill
  7. Java判断中文汉字和中文符号
  8. 扩展和定制Heritrix2
  9. 如何快速的在Windows自带cmd生产文件的MD5数值
  10. 孤寡程序猿找女朋友的方法论