這題一開始做出來沒被AC很鬱悶,後來人家提到1個字符的時候我才發現,我定義的函數初始Power是0,一個字符的時候會除0出現這種 Inf 錯誤
思路就是模擬下哈夫曼編碼樹即可拿到每一個編碼的個數求出總個數
已經被AC
# include<iostream>
#include<iomanip>
# include<string>
# include<map>
# include<queue>
using namespace std;
struct node {
char a;
int pow;
node*lc;
node*rc;
};
struct cmp {
bool operator()(node&a, node&b) {
return a.pow > b.pow;
}
};
priority_queue<node,vector<node>,cmp > pq;
map<char, int>Map;
void Travel(node*R, int Power) {
if (!R)return;
Travel(R->lc, Power + 1);
Travel(R->rc, Power + 1);
map<char, int>::iterator it;
if (!R->lc && !R->rc) {
it = Map.find(R->a);
if (it != Map.end())it->second = Power?Power:1;//開始沒注意1個字符Inf問題
}
}
int main(void) {
string s;
cin >> s;
while (strcmp(s.c_str(),"END")) {
map<char, int>::iterator it;
for (int i = 0; i < s.length(); i++) {
it = Map.find(s[i]);
if (it != Map.end())
(it->second)++;
else
Map.insert(make_pair(s[i], 1));
}//插入不重複的map中統計各個符號個數(權值)
for (it=Map.begin(); it != Map.end(); it++) {
node temp;
temp.a = it->first; temp.pow = it->second; temp.lc = temp.rc = NULL;
pq.push(temp);//進優先隊列中去
}//拿到每個字母的權值進優先隊列
node *Root=NULL;
while (!pq.empty()) {
node *LC; node*RC; node* R; LC = RC = R = NULL;
LC = (node*)malloc(sizeof(node)); if (!LC)exit(-1);
RC = (node*)malloc(sizeof(node)); if (!RC)exit(-1);
R = (node*)malloc(sizeof(node)); if (!R)exit(-1);
*LC = pq.top(); pq.pop();
if (pq.empty()) { Root = LC; free(R); free(RC); break; }
*RC = pq.top(); pq.pop();
R->lc = LC; R->rc = RC; R->a = ' '; R->pow = LC->pow + RC->pow;
pq.push(*R);
}//構造哈夫曼樹
Travel(Root,0);//拿到哈夫曼編碼個數
int res1, res2; double res3; res1 = res2 = 0; res3 = 0.0;
for (int i = 0; i < s.length(); i++) {
it = Map.find(s[i]);
if (it != Map.end()) {//一定可以找到,如果找不到前面出了問題
res2 += it->second;
}
}
res1 = 8 * s.length(); res3 = res1 / (double)res2;
cout << res1 << " " << res2 << " ";
cout.setf(ios::fixed);
cout << setprecision(1) << res3 << endl;
//---------------------------------初始化Map和pq
it = Map.begin();
map<char, int>::iterator itt;
itt = it;
while (it != Map.end()) {
it++; Map.erase(itt); itt = it;
}
while (!pq.empty())pq.pop();
//---------------------------------
cin >> s;
}
system("pause");
return 0;
}