关注
#include <iostream> #include <queue> #include <map> #include <climits> // for CHAR_BIT #include <iterator> #include <algorithm> #include <vector> #include <string> using namespace std; const int UniqueSymbols = 1 << CHAR_BIT; string SampleString; typedef std::vector<bool> HuffCode; typedef std::map<char, HuffCode> HuffCodeMap; class INode { public: const int f; virtual ~INode() {} protected: INode(int f) : f(f) {} }; class InternalNode : public INode { public: INode *const left; INode *const right; InternalNode(INode* c0, INode* c1) : INode(c0->f + c1->f), left(c0), right(c1) {} ~InternalNode() { delete left; delete right; } }; class LeafNode : public INode { public: const char c; LeafNode(int f, char c) : INode(f), c(c) {} }; struct NodeCmp { bool operator()(const INode* lhs, const INode* rhs) const { return lhs->f > rhs->f; } }; INode* BuildTree(const int(&frequencies)[UniqueSymbols]) { std::priority_queue<INode*, std::vector<INode*>, NodeCmp> trees; for (int i = 0; i < UniqueSymbols; ++i) { if (frequencies[i] != 0) trees.push(new LeafNode(frequencies[i], (char)i)); } while (trees.size() > 1) { INode* childR = trees.top(); trees.pop(); INode* childL = trees.top(); trees.pop(); INode* parent = new InternalNode(childR, childL); trees.push(parent); } return trees.top(); } void GenerateCodes(const INode* node, const HuffCode& prefix, HuffCodeMap& outCodes) { if (const LeafNode* lf = dynamic_cast<const LeafNode*>(node)) { outCodes[lf->c] = prefix; } else if (const InternalNode* in = dynamic_cast<const InternalNode*>(node)) { HuffCode leftPrefix = prefix; leftPrefix.push_back(false); GenerateCodes(in->left, leftPrefix, outCodes); HuffCode rightPrefix = prefix; rightPrefix.push_back(true); GenerateCodes(in->right, rightPrefix, outCodes); } } int main() { // Build frequency table int frequencies[UniqueSymbols] = { 0 }; std::cin >> SampleString; for (int i = 0; i < SampleString.size(); i++) { if(SampleString[i] != '\0') ++frequencies[SampleString[i]]; } INode* root = BuildTree(frequencies); HuffCodeMap codes; GenerateCodes(root, HuffCode(), codes); delete root; for (int i = 0; i < SampleString.size(); i++) { for (HuffCodeMap::const_iterator it = codes.begin(); it != codes.end(); ++it) { if(it->first== SampleString[i]) std::copy(it->second.begin(), it->second.end(), std::ostream_iterator<bool>(std::cout)); } } std::cout << std::endl; return 0; }
查看原帖
点赞 评论
相关推荐
牛客热帖
正在热议
# 和牛牛一起刷题打卡 #
4119次浏览 400人参与
# 机械制造薪资爆料 #
340351次浏览 4037人参与
# 牛客帮帮团来啦!有问必答 #
977706次浏览 14952人参与
# 通信硬件薪资爆料 #
241405次浏览 2276人参与
# 腾讯工作体验 #
146079次浏览 1405人参与
# 如何写一份好简历 #
300099次浏览 4320人参与
# 你的简历改到第几版了 #
322896次浏览 4851人参与
# 晒一晒我的offer #
3646786次浏览 56887人参与
# 2022毕业生求职现身说法 #
20218次浏览 307人参与
# 产品人专业大盘点 #
15034次浏览 120人参与
# 浅聊一下我实习的辛苦费 #
93021次浏览 912人参与
# 毕业租房也有小确幸 #
31341次浏览 1710人参与
# 实习必须要去大厂吗? #
17855次浏览 262人参与
# 为什么国企只招应届生 #
55786次浏览 406人参与
# 你觉得机械有必要实习吗 #
10016次浏览 130人参与
# 视觉/交互/设计岗位评价 #
3346次浏览 59人参与
# 为什么那么多公司毁约 #
54523次浏览 481人参与
# 投了多少份简历才上岸 #
66313次浏览 1044人参与
# 2022毕业的你对23届的寄语 #
16178次浏览 347人参与
# 通信/硬件的薪资开多少,才值得去? #
12313次浏览 150人参与