题目来源:http://acm.pku.edu.cn/JudgeOnline/problem?id=1521 解题报告: 典型HUFFMAN树的问题,用了STL的priority_queue,调了一下午,郁闷 #include <iostream> #include <string> #include <queue> using namespace std; class node{ public: int key; //a,b,c... int count;//频率 int p;//父亲结点 friend bool operator < (const node &a, const node &b) { if(b.count < a.count) return true; else return false; } }; int value(char c) { if(c=='_') return 26; else return(c-'A'); } int main() { string str; cin >> str; while(str!="END") { node c[60]; for(int i=0;i<60;i++) { c[i].key=i; c[i].count=0; } int length=str.length(); priority_queue<node> q; for(int i=0;i<length;i++) { (c[value(str.at(i))]).count++; } for(int i=0;i<=26;i++) { if(c[i].count!=0) q.push(c[i]); } if(q.size()==1) { printf("%d %d 8.0/n",8*length,length); } else { int n=27; while(q.size()>1) { node s1=q.top(); q.pop(); node s2=q.top(); q.pop(); c[n].count=s1.count+s2.count; c[s1.key].p=n; c[s2.key].p=n; q.push(c[n]); n++; } int cnt=0; for(int i=0;i<&