← 返回 twosigma 的题目列表Binary Encoding and Decoding
类型:online_judge
Given a string, implement encoding and decoding functions.
Encoding
Start by constructing leaf nodes from the least frequent characters, with parents being the sum of the frequencies of left and right leaf nodes. The value of the node is the character. Construct a binary tree in a specific order. The branch values are 0 and 1. Once the tree is constructed, collect edge values from the root to the leaf node (e.g., 01001) to represent the binary encoding for that leaf.
Decoding
Restore the binary representation back to the original string.
Example
Input
hello