Advertisement
Al3XS0n

Untitled

Nov 24th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1.  
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9. typedef unsigned char byte;
  10.  
  11. pair<int, int> nodes[3000]; // pair<left son, right son>
  12. int BigToSmall[3000]; // from int to char
  13. char SmallToBig[256]; // from char to int
  14.  
  15. string code[256];
  16.  
  17. void Codding(int currentId, string chip = "") {
  18. //cout << currentId << endl;
  19. if (BigToSmall[currentId] != -1) {
  20. code[BigToSmall[currentId]] = chip;
  21. return;
  22. }
  23. Codding(nodes[currentId].first, chip + "0");
  24. Codding(nodes[currentId].second, chip + "1");
  25. }
  26.  
  27. void Encode(string text1, string &text2) {
  28. memset(SmallToBig, -1, sizeof SmallToBig);
  29. memset(BigToSmall, -1, sizeof BigToSmall);
  30. byte InputChar = 0;
  31. vector<int> freqOfChar;
  32. string originalText = "", compressedText = "";
  33.  
  34. freqOfChar.resize(256, 0);
  35. originalText = text1;
  36. for (int i = 0; i < originalText.size(); ++i) {
  37. freqOfChar[originalText[i]]++;
  38. }
  39.  
  40. int countOfNodes = 0;
  41. vector<pair<int, int>> buffer; // pair<node id, node val>
  42. for (size_t i = 0; i < 256; ++i) {
  43. if (freqOfChar[i] != 0) {
  44. countOfNodes++;
  45. BigToSmall[countOfNodes] = i;
  46. SmallToBig[i] = countOfNodes;
  47. buffer.push_back(make_pair(countOfNodes, freqOfChar[i]));
  48. }
  49. }
  50.  
  51. while (buffer.size() > 1) {
  52. sort(buffer.begin(), buffer.end());
  53. reverse(buffer.begin(), buffer.end());
  54.  
  55. pair<int, int> left, right;
  56. left = buffer.back();
  57. buffer.pop_back();
  58. right = buffer.back();
  59. buffer.pop_back();
  60. int currentId = ++countOfNodes;
  61.  
  62. pair<int, int> newNode = make_pair(currentId, left.second + right.second);
  63.  
  64. buffer.push_back(newNode);
  65. nodes[currentId] = make_pair(left.first, right.first);
  66. }
  67.  
  68. Codding(countOfNodes);
  69.  
  70. for (auto currentDiggit : originalText) {
  71. compressedText = compressedText + code[currentDiggit];
  72. }
  73. text2 = compressedText;
  74. }
  75.  
  76. void Decode(string text1, string &text2) {
  77. byte InputChar = 0;
  78. string compressedText = "", originalText = "";
  79.  
  80. compressedText = text1;
  81.  
  82. string currentCode = "";
  83. for (size_t i = 0; i < compressedText.size(); ++i) {
  84. currentCode += compressedText[i];
  85. for (size_t dig = 0; dig < 256; ++dig) {
  86. if (code[dig] == currentCode) {
  87. byte diggit = (byte)dig;
  88. originalText += diggit;
  89. currentCode = "";
  90. break;
  91. }
  92. }
  93. }
  94.  
  95. text2 = originalText;
  96. }
  97.  
  98. int main () {
  99. string orig, comp, s;
  100. getline(cin, orig);
  101. cout << orig << endl;
  102. Encode(orig, comp);
  103. cout << comp << endl;
  104. Decode(comp, s);
  105. cout << s << endl;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement