Description
美国数学家David Huffman在1952年发明了赫夫曼编码,在编码中用到的特殊的二叉树称为Huffman tree。
给出n个字符在文本中的出现次数,输出文本的Huffman编码长度(即编码后的二进制序列有多少位)。
例如,5个字符出现的次数如下,采用对应Huffman编码可得到最优编码长度为4×5+3×15+1×40+2×30+4×10=205。
对应的Huffman Tree如下:
Input
输入的第1行是字符个数n(0<n<=52);以下n行每行为一个字符c及其出现的次数w(c),之间用一个空格分隔。字符只可能为大小写英文字母,输入保证n个字符各不相同。
Output
单独一行输出Huffman编码长度。
Sample Input
5 A 5 B 15 C 40 D 30 E 10
Sample Output
205
// 编码不唯一
#include <iostream> #include <deque> #include <algorithm> #include <string> using namespace std; string str = ""; int sum = 0; //int sequence = 1; struct Node { int value; char ch; Node *left; Node *right; //int order; } *ptr, node[100]; void inorder(Node *sub_root) // 后序遍历 { if (sub_root != NULL) { str += '0'; inorder(sub_root->left); int len1 = str.length() - 1; str.erase(len1); str += '1'; inorder(sub_root->right); int len2 = str.length() - 1; str.erase(len2); if (sub_root->ch != '@') { cout << sub_root->ch << " " << str << endl; sum += sub_root->value * len2; } } } deque<Node*> forest; bool compare(Node *a, Node *b) { return a->value <= b->value; } bool cmp(Node a, Node b) { return a.value < b.value; } int frequency[100] = {0}; char words[100]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { ptr = new Node; cin >> node[i].ch >> node[i].value; ptr->ch = node[i].ch; ptr->value = node[i].value; // ptr->order = 0; ptr->left = NULL; ptr->right = NULL; forest.push_back(ptr); } // deque<Node*>::iterator iter; //sort(node, node + n, cmp); for (int i = 0; i < n - 1; i++) { stable_sort(forest.begin(),forest.end(),compare); /*for (iter = forest.begin(); iter != forest.end(); iter++) { cout << (*iter)->ch << (*iter)->value << " "; } cout << endl;*/ ptr = new Node; ptr->value = forest[0]->value + forest[1]->value; /*if (forest[0]->value < forest[1]-> value || forest[0]->order <= forest[1]->order) { ptr->left = forest[0]; ptr->right = forest[1]; } else { ptr->left = forest[1]; ptr->right = forest[0]; }*/ ptr->left = forest[0]; ptr->right = forest[1]; ptr->ch = '@'; // ptr->order = sequence++; forest.pop_front(); forest.pop_front(); forest.push_back(ptr); } //ptr = forest.front(); inorder(ptr); cout << "sum of bits: " << sum << endl; }