加密与压缩,霍夫曼编码解码

简介:

通过统计一篇文章(或一本书)中每个字符出现的频率(比如字母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 [email protected] and Eva Dou at [email protected]

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

字符: 频率: 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 = freq
        self.left = None
        self.right = None
        self.father = None
    def isLeft(self):
        return self.father.left == self

def 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 = x
        node.right = y

        x.father = node
        y.father = node

        Q.append(node)

    Q[0].father = None
    return 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.father
    return codeList

def 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 + ch
            tmpCode = ""

    return res

def wordCount(strText):
    chDict = {}
    for ch in strText:
        if chDict.get(ch, None):
            chDict[ch] += 1
        else:
            chDict[ch] = 1
    return 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 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 [email protected] and Eva Dou at [email protected]
    """

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]] = code
    print("字符: ", ch[0],"频率: ", ch[1], "编码: ", code)

code = "1110001000000111010010100000111010101000010001111111010010011"
res = huffmanDecode(code, codingDict)
print("decode: ", code, "=>", res)

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

时间: 2024-10-19 14:18:52

加密与压缩,霍夫曼编码解码的相关文章

霍夫曼编码/译码器

赫夫曼树的应用 1.哈夫曼编码 在数据通信中,需要将传送的文字转换成二进制的字符串,用0,1码的不同排列来表示字符.例如,需传送的报文为"AFTER DATA EAR ARE ART AREA",这里用到的字符集为"A,E,R,T,F,D",各字母出现的次数为{8,4,5,3,1,1}.现要求为这些字母设计编码.要区别6个字母,最简单的二进制编码方式是等长编码,固定采用3位二进制,可分别用000.001.010.011.100.101对"A,E,R,T,F

C语言之霍夫曼编码学习

?1,霍夫曼编码描述哈夫曼树─即最优二叉树,带权路径长度最小的二叉树,经常应用于数据压缩. 在计算机信息处理中,"哈夫曼编码"是一种一致性编码法(又称"熵编码法"),用于数据的无损耗压缩.这一术语是指使用一张特殊的编码表将源字符(例如某文件中的一个符号)进行编码.这张编码表的特殊之处在于,它是根据每一个源字符出现的估算概率而建立起来的(出现概率高的字符使用较短的编码,反之出现概率低的则使用较长的编码,这便使编码之后的字符串的平均期望长度降低,从而达到无损压缩数据的目

基于python的二元霍夫曼编码译码详细设计

一.设计题目 对一幅BMP格式的灰度图像(个人证件照片)进行二元霍夫曼编码和译码 二.算法设计 (1)二元霍夫曼编码: ①:图像灰度处理: 利用python的PIL自带的灰度图像转换函数,首先将彩色图片转为灰度的bmp图像,此时每个像素点可以用单个像素点来表示. ②:二元霍夫曼编码: 程序流程图: 详细设计: 统计像素点频率,首先通过python自带的PIL库的图像像素点读取函数read()获取灰度图像的所有像素点,通过循环遍历每个像素点,将每个出现的像素点值以及其次数以键值对的形式放入到pyt

贪心算法-霍夫曼编码

霍夫曼编码是一种无损数据压缩算法.在计算机数据处理中,霍夫曼编码使用变长编码表对源符号(如文件中的一个字母)进行编码,其中变长编码表是通过一种评估来源符号出现机率的方法得到的,出现机率高的字母使用较短的编码,反之出现机率低的则使用较长的编码,这便使编码之后的字符串的平均长度.期望值降低,从而达到无损压缩数据的目的.例如,在英文中,e的出现机率最高,而z的出现概率则最低.当利用霍夫曼编码对一篇英文进行压缩时,e极有可能用一个比特来表示,而z则可能花去25个比特(不是26).用普通的表示方法时,每个

霍夫曼编码求节省空间

霍夫曼编码将频繁出现的字符采用短编码,出现频率较低的字符采用长编码.具体的操作过程为:i)以每个字符的出现频率作为关键字构建最小优先级队列:ii)取出关键字最小的两个结点生成子树,根节点的关键字为孩子节点关键字之和,并将根节点插入到最小优先级队列中,直至得到一棵最优编码树. 霍夫曼编码方案是基于______策略的.用该方案对包含a到f6个字符的文件进行编码,文件包含100000个字符,每个字符的出现频率(用百分比表示)如表1-3所示,则与固定长度编码相比,该编码方案节省了______存储空间.

霍夫曼编码

进行霍夫曼编码前,我们先创建一个霍夫曼树. ⒈将每个英文字母依照出现频率由小排到大,最小在左,如Fig.1. ? ? ⒉每个字母都代表一个终端节点(叶节点),比较F.O.R.G.E.T五个字母中每个字母的出现频率,将最小的两个字母频率相加合成一个新的节点.如Fig.2所示,发现F与O的频率最小,故相加2+3=5. ⒊比较5.R.G.E.T,发现R与G的频率最小,故相加4+4=8. ⒋比较5.8.E.T,发现5与E的频率最小,故相加5+5=10. ⒌比较8.10.T,发现8与T的频率最小,故相加8

霍夫曼树及霍夫曼编码的C语言实现

从周五开始学习霍夫曼树,一直到今天终于完成,期间遇到了各种各样的棘手的问题,通过一遍遍在纸上分析每一步的具体状态得以解决.现在对学习霍夫曼树的过程加以记录 首先介绍霍夫曼树 霍夫曼树(Huffman Tree),又称最优二叉树,是一类带权路径长度最短的树.假设有n个权值{w1,w2,-,wn},如果构造一棵有n个叶子节点的二叉树,而这n个叶子节点的权值是{w1,w2,-,wn},则所构造出的带权路径长度最小的二叉树就被称为赫夫曼树. 这里补充下树的带权路径长度的概念.树的带权路径长度指树中所有叶

采用霍夫曼编码(Huffman)画出字符串各字符编码的过程并求出各字符编码 --多媒体技术与应用

题目:有一个字符串:cabcedeacacdeddaaaba,问题: (1)采用霍夫曼编码画出编码的过程,并写出各字符的编码 (2)根据求得的编码,求得各编码需要的总位数 (3)求出整个字符串总编码长度,并计算出字符串位数在编码前与编码后的比值 解答: (1)各字符出现频率统计如下表所示. |符号 |出现次数 |出现频率| |--|--|--| | a |7|0.35| |b|2|0.1| |c|4|0.2| |d|4|0.2| |e|3|0.15| 编码过程如下图所示: 各字符编码如下表所示:

优先级队列优化的霍夫曼编码(带中文压缩)

利用STL中的优先级队列进行优化 我将压缩和解压分为两部分,其实一些还是是一样的 压缩的时候通过bitset将每8个01串压缩成一个字节,如果最后一个不满足8个,用0补齐,但是要记录最后一个字节实际有多少个有效位,将其存入文件最后一个字节,解压的时候先将文件定位到最后一个字节,取出有效位的个数,压缩文件真正有效的是倒数第二个字节,倒数第一个字节只是记录倒数第二个字节中有几位是有效的,解压的时候根据密码本(记录每个字节的权值)建立哈夫曼树,然后更具哈夫曼树解压文件 压缩代码部分: #include