Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 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. bool ReadBit(int& ptr, int& bitIt, vector<byte>& inputData){
  118. bool res = ((inputData[ptr] >> (8 - bitIt)) & 1) == 1;
  119. bitIt++;
  120. if(bitIt == 9){
  121. bitIt = 1;
  122. ptr++;
  123. }
  124. return res;
  125. }
  126.  
  127. int main(){
  128. vector<byte> data;
  129. byte cursor;
  130. // int n;
  131. // cin >> n;
  132. // for(int i = 0; i < n; ++i){
  133. // cin >> cursor;
  134. // data.push_back(cursor);
  135. // }
  136. while(cin >> cursor){
  137. data.push_back(cursor);
  138. }
  139. vector<byte> symbols(256);
  140. int size = data.size();
  141. for(int i = 0; i < size; ++i){
  142. symbols[(int)data[i]]++;
  143. }
  144. vector<Node*> usedSymbols;
  145. int it = 0;
  146. Node* newNode = nullptr;
  147. for(int i = 0; i < 256; ++i){
  148. if(symbols[i] > 0){
  149. newNode = new Node((byte)i, symbols[i]);
  150. usedSymbols.push_back(newNode);
  151. it = usedSymbols.size() - 1;
  152. while(it > 0 && usedSymbols[it]->freq > usedSymbols[it - 1]->freq){
  153. swap(usedSymbols[it], usedSymbols[it - 1]);
  154. it--;
  155. }
  156. }
  157. }
  158. Node* node1;
  159. Node* node2;
  160. while(usedSymbols.size() > 1){
  161. node1 = usedSymbols.back();
  162. usedSymbols.pop_back();
  163. node2 = usedSymbols.back();
  164. usedSymbols.pop_back();
  165. newNode = new Node(node1, node2);
  166. usedSymbols.push_back(newNode);
  167. it = usedSymbols.size() - 1;
  168. while(it > 0 && usedSymbols[it]->freq > usedSymbols[it - 1]->freq){
  169. swap(usedSymbols[it], usedSymbols[it - 1]);
  170. it--;
  171. }
  172. }
  173. FixSize(usedSymbols[0]);
  174. map<byte, vector<bool> > mp;
  175. byte treeSize = BFSCalc(usedSymbols[0], mp);
  176. // State(usedSymbols[0]);
  177. cout << '\n';
  178. vector<byte> outputData;
  179. outputData.push_back(treeSize);
  180. outputData.push_back(0);
  181. int bitIt = 1;
  182. map<byte, vector<bool> >::iterator iter = mp.begin();
  183. for(; iter != mp.end(); ++iter){
  184. WriteByte(iter->first, bitIt, outputData);
  185. WriteByte((byte)iter->second.size(), bitIt, outputData);
  186. for(int i = 0; i < iter->second.size(); ++i){
  187. WriteBit(iter->second[i], bitIt, outputData);
  188. }
  189. }
  190. for(int i = 0; i < data.size(); ++i){
  191. for(int j = 0; j < mp[data[i]].size(); ++j){
  192. WriteBit(mp[data[i]][j], bitIt, outputData);
  193. }
  194. }
  195. outputData.push_back((byte)bitIt);
  196. delete usedSymbols[0];
  197. for(int i = 0; i < outputData.size(); ++i){
  198. cout << outputData[i];
  199. }
  200. vector<byte> inputData = outputData;
  201. int ptr = 0;
  202. // byte treeSize;
  203. treeSize = inputData[0];
  204. ptr++;
  205. byte curChar = 0;
  206. byte curLength = 0;
  207. // int bitIt = 0;
  208. bitIt = 1;
  209. Node* root = new Node;
  210. Node* treePtr;
  211. for(int i = 0; i < (int)treeSize; ++i){
  212. curChar = 0;
  213. curLength = 0;
  214. treePtr = root;
  215. for(int j = 7; j >= 0; --j){
  216. if(ReadBit(ptr, bitIt, inputData)){
  217. curChar |= 1 << j;
  218. }
  219. }
  220. for(int j = 7; j >= 0; --j){
  221. if(ReadBit(ptr, bitIt, inputData)){
  222. curLength |= 1 << j;
  223. }
  224. }
  225. for(int j = 0; j < (int)curLength; ++j){
  226. if(ReadBit(ptr, bitIt, inputData)){
  227. if(treePtr->rchild == nullptr){
  228. treePtr->rchild = new Node;
  229. treePtr = treePtr->rchild;
  230. }
  231. else{
  232. treePtr = treePtr->rchild;
  233. }
  234. }
  235. else{
  236. if(treePtr->lchild == nullptr){
  237. treePtr->lchild = new Node;
  238. treePtr = treePtr->lchild;
  239. }
  240. else{
  241. treePtr = treePtr->lchild;
  242. }
  243. }
  244. }
  245. treePtr->symbol = curChar;
  246. }
  247. vector<byte> result;
  248. treePtr = root;
  249. // State(root);
  250. while(!((ptr == inputData.size() - 2) && bitIt == (int)inputData[inputData.size() - 1])){
  251. while(treePtr->lchild != nullptr){
  252. treePtr = (ReadBit(ptr, bitIt, inputData))?treePtr->rchild:treePtr->lchild;
  253. }
  254. result.push_back(treePtr->symbol);
  255. // cout << treePtr->symbol;
  256. // cout << "here" << '\n';
  257. treePtr = root;
  258. }
  259. for(int i = 0; i < result.size(); ++i){
  260. cout << result[i];
  261. }
  262. return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement