Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <string>
  5. #include <utility>
  6. #include <memory>
  7. #include <algorithm>
  8.  
  9. using std::string;
  10. using std::cout;
  11. using std::cin;
  12. using std::endl;
  13. using std::cerr;
  14. using std::vector;
  15. using std::map;
  16. using std::shared_ptr;
  17.  
  18. struct Huffman_Node;
  19.  
  20. using Huffman_Node_ptr = shared_ptr<Huffman_Node>;
  21. using Haffman_pair_of_ptr = std::pair<Huffman_Node_ptr, Huffman_Node_ptr>;
  22.  
  23. struct Huffman_Node {
  24.     Haffman_pair_of_ptr pair;
  25.     int count;
  26.     char char_of_node;
  27.    
  28.     bool isLeaf() const {
  29.         return pair.first == nullptr &&
  30.             pair.second == nullptr;
  31.     }
  32. };
  33.  
  34. bool operator<(const Huffman_Node& left, const Huffman_Node& right) {
  35.     return left.count < right.count;
  36. }
  37.  
  38. bool Huffman_compression(vector<Huffman_Node>& vec) {
  39.     int size_ = vec.size();
  40.     if (size_ <= 1) return false;
  41.     std::sort(vec.rbegin(), vec.rend());
  42.     Huffman_Node new_node{
  43.         Haffman_pair_of_ptr(std::make_shared<Huffman_Node>(vec[size_ - 1]),
  44.     std::make_shared<Huffman_Node>(vec[size_ - 2])),
  45.     vec[size_ - 1].count + vec[size_ - 2].count,
  46.     0
  47.     };
  48.     vec.pop_back();
  49.     vec.pop_back();
  50.     vec.push_back(std::move(new_node));
  51.     return true;
  52. }
  53.  
  54. void Huffman_node_dfs_to_map(const Huffman_Node& node, string code_of_node, map<char, string>& Huffman_map) {
  55.     if (!node.isLeaf()) {
  56.         Huffman_node_dfs_to_map(*(node.pair.first), code_of_node + '1', Huffman_map);
  57.         Huffman_node_dfs_to_map(*(node.pair.second), code_of_node + '0', Huffman_map);
  58.     }
  59.     else {
  60.         Huffman_map[node.char_of_node] = code_of_node;
  61.     }
  62. }
  63.  
  64.  
  65. int main() {
  66.     string str;
  67.     vector<int> count_of_char(26, 0);
  68.     std::cin >> str;
  69.     for (auto& c : str) {
  70.         ++count_of_char[c - 'a'];
  71.     }
  72.     vector<Huffman_Node> Huffman_Nodes;
  73.     for (int i = 0; i != 26; ++i) {
  74.         if (int count = count_of_char[i]) {
  75.             Huffman_Node node{
  76.                 Haffman_pair_of_ptr(nullptr,nullptr),
  77.                 count,
  78.                 static_cast<char>(i + 'a')
  79.             };
  80.             Huffman_Nodes.push_back(std::move(node));
  81.         }
  82.     }
  83.     while (Huffman_compression(Huffman_Nodes)) {}
  84.  
  85.     map<char, string> Huffman_map;
  86.     Huffman_node_dfs_to_map(Huffman_Nodes[0], "", Huffman_map);
  87.  
  88.     if (Huffman_map.size() == 1) {
  89.         Huffman_map.begin()->second = "0";
  90.     }
  91.  
  92.     string code;
  93.     for (auto& c : str) {
  94.         code += Huffman_map[c];
  95.     }
  96.  
  97.     cout << Huffman_map.size() << ' ' << code.size() << endl;
  98.  
  99.     for (auto& s : Huffman_map) {
  100.         cout << s.first << ": " << s.second << endl;
  101.     }
  102.     cout << code;
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement