Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1053
Analysis: this topic is a typical problem of the user tree. In a word, the main function of the user tree is to solve the problem of compression encoding, I believe that all students who have studied discrete mathematics should have learned this magical data structure.
(1) create a structure of a node in the Harman tree
TypedefStructHuffman_trie {IntDeep;//DepthIntFreq;//Frequency (that is, the weight in the Harman tree)Huffman_trie * left ,*Right;//The method used for sorting and comparison in the priority queue. If you do not understand it, we recommend that you first learn the priority queue.FriendBool Operator<(Huffman_trie A, huffman_trie B)ReturnA. freq>B. freq;} huffman_trie;
(2) First, we pre-process the read strings and place them into the corresponding nodes according to the characters. At the same time, we record the occurrence frequency, count_num record the occurrence times of the characters, and put them into the Harman node.
Len =Strlen (STR); STR [Len] = ' ! ' ; Sort (STR, STR + Len); count_num = 1 ; Index = 0 ; For ( Int I = 1 ; I <= Len; I ++ ){ If (STR [I]! = STR [I- 1 ]) {Trie [Index ++]. Freq = Count_num; count_num = 1 ;} Else Count_num ++ ;}
(3) Use a priority queue to store nodes. Extract the two nodes with the least frequency each time, merge the nodes, and put the merged nodes into the priority queue, only the last node is left in the queue and used as the root node. (This is the key to building the Harman tree, which must be understood here)
Root = (huffman_trie *) malloc ( Sizeof (Huffman_trie )); For (Int I = 0 ; I <index; I ++ ) PQ. Push (trie [I]); While (PQ. Size ()> 1 ) {Huffman_trie * H1 = (huffman_trie *) malloc ( Sizeof (Huffman_trie )); * H1 = PQ. Top (); PQ. Pop (); huffman_trie * H2 = (huffman_trie *) malloc ( Sizeof (Huffman_trie )); * H2 =PQ. Top (); PQ. Pop (); huffman_trie H3; h3.left = H1; h3.right = H2; h3.freq = H1-> freq + H2-> Freq; PQ. Push (H3 );} * Root = PQ. Top ();
(4) After the establishment of the Harman tree, it is the problem of coding length. At this time, it is the problem of traversing the tree. There are many methods that can be traversed in depth or in width. Here we use the breadth traversal and a queue.
Queue Q; q. Push ( * Root ); While (! Q. Empty () {huffman_trie HT = Q. Front (); q. Pop (); If (HT. Left! = Null) {Ht. Left -> Deep = Ht. Deep + 1 ; Q. Push ( * Ht. Left );} If (HT. Right! = Null) {Ht. Right -> Deep = Ht. Deep + 1 ; Q. Push ( * Ht. Right );} If (! Ht. Left &&! Ht. Right) sum + = Ht. Deep * Ht. freq ;}
(5) The rest is very simple. I will not repeat it too much.
All questionsCode
# Include <iostream> # Include <Queue> # Include <Cstdio> # Include <Cstring> # Include <Algorithm> Using Namespace STD; typedef Struct Huffman_trie { Int Deep;// Depth Int Freq; // Frequency (that is, the weight in the Harman tree) Huffman_trie * left ,* Right; // The method used for sorting and comparison in the priority queue. If you do not understand it, we recommend that you first learn the priority queue. Friend Bool Operator < (Huffman_trie A, huffman_trie B) Return A. freq> B. freq;} huffman_trie; huffman_trie trie [ 300 ]; // Harman Tree node Huffman_trie * Root; Int Len, count_num, index, sum; priority_queue <Huffman_trie> PQ; Void Huffman () {sum = 0 ; Root = (Huffman_trie *) malloc ( Sizeof (Huffman_trie )); For (Int I = 0 ; I <index; I ++ ) PQ. Push (trie [I]); While (PQ. Size ()> 1 ) {Huffman_trie * H1 = (huffman_trie *) malloc ( Sizeof (Huffman_trie )); * H1 = PQ. Top (); PQ. Pop (); huffman_trie * H2 = (huffman_trie *) malloc ( Sizeof (Huffman_trie )); * H2 =PQ. Top (); PQ. Pop (); huffman_trie H3; h3.left = H1; h3.right = H2; h3.freq = H1-> freq + H2-> Freq; PQ. Push (H3 );} * Root = PQ. Top (); PQ. Pop (); root -> Deep = 0 ; Queue <Huffman_trie> Q; q. Push ( * Root ); While (! Q. Empty () {huffman_trie HT =Q. Front (); q. Pop (); If (HT. Left! = Null) {Ht. Left -> Deep = Ht. Deep + 1 ; Q. Push ( * Ht. Left );} If (HT. Right! = Null) {Ht. Right -> Deep = Ht. Deep + 1 ; Q. Push ( * Ht. Right );} If (! Ht. Left &&!Ht. Right) sum + = Ht. Deep * Ht. freq ;}} Int Main (){ Char STR [ 1000 ]; While (Scanf ( " % S " , STR )! = EOF & strcmp (STR, " End " )! = 0 ) {Len = Strlen (STR); STR [Len] = ' ! ' ; Sort (STR, STR + Len); count_num = 1 ; Index = 0 ; For ( Int I = 1 ; I <= Len; I ++){ If (STR [I]! = STR [I- 1 ]) {Trie [Index ++]. Freq = Count_num; count_num = 1 ;} Else Count_num ++ ;} If (Index = 1 ) Printf ( " % D % D 8.0 \ n " , Len * 8 , Len ); Else {Huffman (); printf ( " % D %. 1lf \ n " , Len * 8 , Sum, Len * 8 * 1.0 / Sum );}} Return 0 ;}