[Programming Questions-American group] character encoding

Source: Internet
Author: User

[Programming questions] character encoding design an algorithm that gives a binary encoding of a string so that the length of the encoded string is the shortest.

Input Description:
One row for each set of data, for the string to encode. guarantees that the string length is less than or equal to 1000.


Output Description:
The shortest encoded length of a line output.

Input Example:
Mt-tech-team

Output Example:
33
#include <cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#include<utility>#include<queue>using namespacestd;intMain () {Chars[3300];  while(SCANF ("%s", s)! =EOF) {        intn =strlen (s); Sort (s,s+N); Priority_queue<int> heap;//priority_queue default is the Big Top heap        intCNT =0;  for(inti =0, J;i <N;) {J=i;  while(J < n && s[j] = = S[i]) + +J; Heap.push (i-j); I=J; ++CNT; }        intRET =0;  for(inti =0; I < CNT-1;++i) {            intA =heap.top (); Heap.pop (); intB =heap.top (); Heap.pop (); RET-= A +B; Heap.push (A+B); } printf ("%d\n", ret); }    return 0;}

[Programming Questions-American group] character encoding

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.