[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