Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <queue>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. typedef unsigned char byte;
  10.  
  11. struct Node{
  12. byte symbol = '-';
  13. int freq;
  14. vector<bool> path;
  15. int size = 0;
  16. int lvl = 0;
  17. Node* lchild = nullptr;
  18. Node* rchild = nullptr;
  19. Node(Node* _lchild, Node* _rchild) : lchild(_lchild), rchild(_rchild), freq(_rchild->freq + _lchild->freq){}
  20. Node(byte _symbol, int _freq) : symbol(_symbol), freq(_freq){}
  21. Node() = default;
  22. ~Node(){
  23. delete lchild;
  24. delete rchild;
  25. }
  26. };
  27.  
  28. void State(Node* _node){
  29. queue<Node*> q;
  30. q.push(_node);
  31. Node* it;
  32. _node->lvl = 1;
  33. while(q.size()){
  34. it = q.front();
  35. if(it->lchild != nullptr){
  36. q.push(it->lchild);
  37. it->lchild->lvl = it->lvl + 1;
  38. }
  39. if(it->rchild != nullptr){
  40. q.push(it->rchild);
  41. it->rchild->lvl = it->lvl + 1;
  42. }
  43. for(int i = 0; i < it->lvl; ++i){
  44. cout << '\t';
  45. }
  46. cout << it->symbol << '.';
  47. for(int i = 0; i < it->path.size(); ++i){
  48. cout << it->path[i]?1:0;
  49. }
  50. cout << '\n';
  51. q.pop();
  52. }
  53. it = nullptr;
  54. cout << '\n' << '\n';
  55. }
  56.  
  57. void FixSize(Node* _node){
  58. if(_node->lchild == nullptr){
  59. _node->size = 1;
  60. return;
  61. }
  62. FixSize(_node->lchild);
  63. FixSize(_node->rchild);
  64. _node->size = _node->lchild->size + _node->rchild->size + 1;
  65. }
  66.  
  67. void CalcPath(Node* _node){
  68. if(_node->lchild == nullptr){
  69. return;
  70. }
  71. _node->lchild->path = _node->path;
  72. _node->lchild->path.push_back(false);
  73. _node->rchild->path = _node->path;
  74. _node->rchild->path.push_back(true);
  75. }
  76.  
  77. byte BFSCalc(Node* _node, map<byte, vector<bool> >& mp){
  78. byte treeSize = 0;
  79. queue<Node*> q;
  80. q.push(_node);
  81. Node* it = nullptr;
  82. while(!q.empty()){
  83. it = q.front();
  84. q.pop();
  85. if(it->lchild != nullptr){
  86. CalcPath(it);
  87. q.push(it->lchild);
  88. q.push(it->rchild);
  89. }
  90. else{
  91. treeSize++;
  92. mp[it->symbol] = it->path;
  93. }
  94. }
  95. return treeSize;
  96. }
  97.  
  98. void WriteBit(bool bit, int& bitIt, vector<byte>& outputData){
  99. if(bit){
  100. outputData.back() |= 1 << (8 - bitIt);
  101. }
  102. bitIt++;
  103. if(bitIt == 9){
  104. outputData.push_back(0);
  105. bitIt = 1;
  106. }
  107. }
  108.  
  109. void WriteByte(byte value, int& bitIt, vector<byte>& outputData){
  110. bool bit;
  111. for(int i = 1; i < 9; ++i){
  112. bit = ((value >> (8 - i)) & 1 == 1);
  113. WriteBit(bit, bitIt, outputData);
  114. }
  115. }
  116.  
  117. int main(){
  118. vector<byte> data;
  119. byte cursor;
  120. // int n;
  121. // cin >> n;
  122. // for(int i = 0; i < n; ++i){
  123. // cin >> cursor;
  124. // data.push_back(cursor);
  125. // }
  126. while(cin >> cursor){
  127. data.push_back(cursor);
  128. }
  129. vector<byte> symbols(256);
  130. int size = data.size();
  131. for(int i = 0; i < size; ++i){
  132. symbols[(int)data[i]]++;
  133. }
  134. vector<Node*> usedSymbols;
  135. int it = 0;
  136. Node* newNode = nullptr;
  137. for(int i = 0; i < 256; ++i){
  138. if(symbols[i] > 0){
  139. newNode = new Node((byte)i, symbols[i]);
  140. usedSymbols.push_back(newNode);
  141. it = usedSymbols.size() - 1;
  142. while(it > 0 && usedSymbols[it]->freq > usedSymbols[it - 1]->freq){
  143. swap(usedSymbols[it], usedSymbols[it - 1]);
  144. it--;
  145. }
  146. }
  147. }
  148. Node* node1;
  149. Node* node2;
  150. while(usedSymbols.size() > 1){
  151. node1 = usedSymbols.back();
  152. usedSymbols.pop_back();
  153. node2 = usedSymbols.back();
  154. usedSymbols.pop_back();
  155. newNode = new Node(node1, node2);
  156. usedSymbols.push_back(newNode);
  157. it = usedSymbols.size() - 1;
  158. while(it > 0 && usedSymbols[it]->freq > usedSymbols[it - 1]->freq){
  159. swap(usedSymbols[it], usedSymbols[it - 1]);
  160. it--;
  161. }
  162. }
  163. FixSize(usedSymbols[0]);
  164. map<byte, vector<bool> > mp;
  165. byte treeSize = BFSCalc(usedSymbols[0], mp);
  166. State(usedSymbols[0]);
  167. cout << '\n';
  168. vector<byte> outputData;
  169. outputData.push_back(treeSize);
  170. outputData.push_back(0);
  171. int bitIt = 1;
  172. map<byte, vector<bool> >::iterator iter = mp.begin();
  173. for(; iter != mp.end(); ++iter){
  174. WriteByte(iter->first, bitIt, outputData);
  175. WriteByte((byte)iter->second.size(), bitIt, outputData);
  176. for(int i = 0; i < iter->second.size(); ++i){
  177. WriteBit(iter->second[i], bitIt, outputData);
  178. }
  179. }
  180. for(int i = 0; i < data.size(); ++i){
  181. for(int j = 0; j < mp[data[i]].size(); ++j){
  182. WriteBit(mp[data[i]][j], bitIt, outputData);
  183. }
  184. }
  185. for(int i = 0; i < outputData.size(); ++i){
  186. for(int j = 1; j < 9; ++j){
  187. cout << (((outputData[i] >> (8 - j)) & 1) == 1)?1:0;
  188. }
  189. }
  190. delete usedSymbols[0];
  191. return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement