Wit 0 Kawasaki not sessile ⅱ (Huffman code, priority queue)

Source: Internet
Author: User

Title Description

You summon the alien creature with joy, thinking that you can become a Superman has the power to conquer all monsters, but facing the tall alien creature in front of you, you have a blank face, because, you know M78 Nebula language? But don't worry, because 0 Kawasaki is very witty, he gave a key hint: "Reason, Japanese is the universal language, so why not try to speak Japanese with aliens?" ”

But now extraterrestrial creatures are saying "[Email protected]#$%^&%#%i&!......" Such things, how do you convert into Japanese?

As a universal Japanese language, the natural set of universal conversion algorithm, that is Huffman code conversion! Of course, this is definitely not an ordinary Huffman encoding conversion, but rather a different kana depending on the encoding length.

Input

The first behavior is an integer t, followed by the T-line data. 1<=t<=100

Each set of input data is an alien string, for convenience, use uppercase and lowercase letters instead of alien letters. String length not exceeding 2000

Output

For each set of data, the output corresponds to the total length of the binary Huffman encoding

Input sample
2abababacabcdefg
Output sample
1220
title Source: Http://biancheng.love/contest/23/problem/D/index
Problem-solving ideas: in front of the Huffman tree structure and Huffman coding and decoding, has described the way Huffman coding. Now we need to get the length of Huffman code. Recall the algorithm in the decoding process, if it is 0 to the left, if the search is 1 to the right. Therefore, a character in a well-constructed Huffman tree is a leaf node. It means the lowest end of each fork in the tree. So the length of each character is the depth of the character in the Huffman tree. Hope to understand this.
after understanding the character length is the character depth begins to re-analyze the Huffman tree construction process. The establishment of Huffman tree by the occurrence of each character frequency statistics to design, different frequency will correspond to different depth is different length.
The setup process is as follows:
1, remove the minimum frequency of two numbers, the two numbers according to the rule of the best binary tree (left child < parent node < right child) to get the value of the parent node, put this value in the above frequency (equivalent to merging two minimum two frequencies)
2, in accordance with the above-mentioned rules of repeated merger and return.
3, Get Huffman tree.
The above analysis gets two minimum frequencies per need. How can we achieve the minimum two frequencies at a time? It can be done by array, but it is cumbersome and time-consuming to sort every time, but it can use the random algorithm of the I-order statistic, but its complexity is also very high. So what structure can we use? It's time to talk about it. Priority queue
procedures for using priority queues:
1, the first need to count the frequency of each letter in the string, because the problem has been simplified, the character is limited to lowercase characters A to lowercase characters z,
2, the initialization of A to Z frequency is 0, so after the frequency frequency of statistics need to use the frequency is not 0 this important condition will appear the character push into the queue.
3, each time the smallest two added and then placed in the priority queue. The condition for exiting the loop is that the priority queue is empty.
Here's the code:
#include <bits/stdc++.h>#defineMax_size 10010using namespaceStd;typedefLong LongLL;CharC[max_size];Long LongF[max_size];p riority_queue<ll,vector<ll>,greater<ll> >q;//build a small top heap;Long LongN,ans;intMain () {intN; scanf ("%d",&N);  while(n--)    {        strings; CIN>>s;        GetChar (); Memset (F,0,sizeof(f)); intlens=s.size ();  for(intI=1; i<=lens; i++) {C[i]=s[i-1]; F[c[i]]++; }         while(!q.empty ()) Q.pop ();  for(intI= $; i<=122; i++)        {            if(f[Char(i)] >0) {sum++; Q.push (f[Char(i)]); }} ans=0;  while(Q.size () >1) {LL a=Q.top ();            Q.pop (); LL b=Q.top ();            Q.pop (); Ans+ = (a+b);//because the encoding length is related to the number of layers in the treeQ.push (A +b); } printf ("%lld\n", ans); }    return 0;}

It can be found that the statistical frequency in front is not only for statistical purposes, but also to push it into the priority queue.

Here is the code to solve the problem according to Huffman tree, you can compare the advantages and disadvantages of the two methods:
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstdlib>5#include <algorithm>6#include <iomanip>7#include <cstring>8#include <string>9 #defineINF 0xFFFFFFTen using namespacestd; One  Atypedefstruct - { -     intparent[1005]; the     intlchild[1005]; -     intrchild[1005]; -     intweight[1005]; - } htree; +  - intCreateht (Htree &ht) + { A     intn =0;//length; at     intmin1,min2; -     intLchild,rchild; -memset (ht.parent,-1,sizeof(ht.parent)); -memset (ht.lchild,-1,sizeof(Ht.lchild)); -memset (ht.rchild,-1,sizeof(Ht.rchild)); -memset (Ht.weight,0,sizeof(Ht.weight)); in  -     stringstr; toCin>>str; +     intuppercase[ -], lowercase[ -]; -memset (Uppercase,0,sizeof(uppercase)); thememset (lowercase,0,sizeof(lowercase)); *      for(inti =0; I<str.length (); i++) $     {Panax Notoginseng         if(str[i]< the)//Uppercase -uppercase[str[i]- $]++; the         Else +lowercase[str[i]- the]++; A     } the      for(intI=0; i< -; i++) +     { -         if(uppercase[i]!=0) $ht.weight[n++] =Uppercase[i]; $     } -      for(intI=0; i< -; i++) -     { the         if(lowercase[i]!=0) -ht.weight[n++] =Lowercase[i];Wuyi     } the  -      for(intI=n; i<2*n-1; i++) Wu     { -Min1=min2=INF; Aboutlchild=rchild=-1; $          for(intj=0; j<i; J + +) -         { -             if(ht.parent[j]==-1) -             { A                 if(ht.weight[j]<min1) +                 { theMin2=min1; -Rchild=Lchild; $min1=Ht.weight[j]; theLchild=J; the                 } the                 Else if(ht.weight[j]<min2) the                 { -Min2=Ht.weight[j]; inRchild=J; the                 } the             } About         } theht.weight[i]=ht.weight[lchild]+Ht.weight[rchild]; theht.lchild[i]=Lchild; theht.rchild[i]=Rchild; +ht.parent[lchild]=ht.parent[rchild]=i; -     } the     returnN;Bayi } the  the intMain () - { -     intT; the Htree ht; the     Long Longresult; the     intLevel ; theCin>>T; -      while(t--) the     { the         intLen =Createht (HT); theresult =0;94          for(inti =0; i<len; i++) the         { theLevel =0; the             intj =i;98              while(ht.parent[j]!=-1) About             { -level++;101j=Ht.parent[j];102             }103result+=level*Ht.weight[i];104         } theprintf"%lld\n", result);106     }107}
View Code



Wit 0 Kawasaki not sessile ⅱ (Huffman code, priority queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.