标签:
哈夫曼树,优先队列
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #define maxn 30+5 using namespace std; string ans; int sum[maxn]; priority_queue<int,vector<int>,greater<int> >mapp; int main() { while(cin>>ans) { if(ans=="END"){break;} memset(sum,0,sizeof(sum)); while(mapp.size()) mapp.pop(); for(int i=0;i<ans.size();i++) { if(ans[i]=='_') sum[0]++; else sum[ans[i]-'A'+1]++; } for(int i=0;i<=26;i++) { if(sum[i]) mapp.push(sum[i]); } int a=0,b=0; if(mapp.size()==1) { printf("%d %d %.1lf\n",ans.size()*8,ans.size(),double(8)); continue; } while(mapp.size()>1) { int x=mapp.top(); mapp.pop(); int y=mapp.top(); mapp.pop(); mapp.push(x+y); a+=x+y; } b=ans.size()*8; double c=double(b)/double(a); printf("%d %d %.1lf\n",b,a,c); } return 0; }
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/45226451