简介:
通过统计一篇文章(或一本书)中每个字符出现的频率(比如字母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)