标签:
每组数据一行,为待编码的字符串。保证字符串长度小于等于1000。
一行输出最短的编码后长度。
MT-TECH-TEAM
33
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <utility> #include <queue> using namespace std; int main(){ char s[3300]; while(scanf("%s",s) != EOF){ int n = strlen(s); sort(s,s + n); priority_queue<int> heap;//priority_queue 默认是 大顶堆 int cnt = 0; for(int i = 0,j;i < n;){ j = i; while(j < n && s[j] == s[i]) ++ j; heap.push(i - j); i = j; ++ cnt; } int ret = 0; for(int i = 0;i < cnt - 1;++ i){ int A = heap.top(); heap.pop(); int B = heap.top(); heap.pop(); ret -= A + B; heap.push(A + B); } printf("%d\n",ret); } return 0; }
标签:
原文地址:http://www.cnblogs.com/learning-c/p/5746701.html