fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cstdint>
  5. #include <random>
  6. #include <limits>
  7. #include <functional>
  8. #include <map>
  9. typedef std::vector<std::uint8_t> BType;
  10. typedef std::vector<BType> Block;
  11.  
  12. //have bug!
  13.  
  14. BType MakeData(std::size_t N = std::numeric_limits<std::uint16_t>::max()){
  15.  
  16. std::random_device RD;
  17. // std::mt19937 mt(RD());
  18. std::mt19937 mt(0);
  19. std::uniform_int_distribution<int> uid(0, 255);
  20.  
  21. BType ret;
  22. for (std::size_t i = 0; i < N; i++){
  23. ret.push_back(uid(mt));
  24. }
  25.  
  26. return ret;
  27. }
  28.  
  29. bool Append(Block& B, BType& Data){
  30.  
  31. B.push_back(Data);
  32.  
  33. return true;
  34. }
  35.  
  36. std::pair<std::size_t,BType> BlockSortEn(BType& Data){
  37. auto T = Data;
  38. Block B;
  39. BType R;
  40. std::size_t j = 0;
  41. for (std::size_t i = 1; i <= T.size(); i++){
  42. Append(B, T);
  43. std::rotate(T.begin(), T.begin()+1, T.end());
  44.  
  45. }
  46. std::sort(B.begin(), B.end());
  47.  
  48. for (std::size_t i = 0; i < B.size(); i++){
  49. R.push_back(B[i][Data.size() - 1]);
  50. if (B[i] == Data) j = i;
  51. }
  52.  
  53. return std::make_pair(j,R);
  54. }
  55.  
  56. BType BlockSortDe(std::pair<std::size_t, BType>& Data){
  57. std::vector<std::pair<std::uint8_t,std::size_t>> T;
  58. BType R;
  59. for (std::size_t i = 0; i < Data.second.size(); i++){
  60. T.push_back(std::make_pair(Data.second[i], i));
  61. }
  62. std::sort(T.begin(), T.end());
  63. std::size_t P = 0;
  64. for (std::size_t i = 0; i < T.size(); i++){
  65. R.push_back(T[P].first);
  66. P = T[P].second;
  67. }
  68.  
  69. if(Data.first !=0)
  70. std::rotate(R.begin(), R.begin() + Data.first-1 , R.end());
  71. return R;
  72.  
  73. }
  74.  
  75. bool MakeHoge(BType D){
  76. auto BSE = BlockSortEn(D);
  77. auto BSD = BlockSortDe(BSE);
  78.  
  79. std::cout << "オリジナル" << std::endl;
  80. for (auto& o : D) std::cout << static_cast<int>(o) << ',';
  81. std::cout << std::endl;
  82. std::cout << "復号化" << std::endl;
  83. for (auto& o : BSD) std::cout << static_cast<int>(o) << ',';
  84. std::cout << std::endl;
  85. std::cout << "符号化@" <<BSE.first<< std::endl;
  86. for (auto& o : BSE.second) std::cout << static_cast<int>(o) << ',';
  87. std::cout << std::endl;
  88.  
  89. if (D == BSD){
  90. std::cout << "データ完全一致!" << std::endl;
  91. }
  92. else{
  93. std::cout << "データ完全一致しませんでした!" << std::endl;
  94. }
  95.  
  96. return true;
  97. }
  98.  
  99. int main(){
  100. static const std::size_t L = 32;
  101. auto D = MakeData(L);
  102. BType A{ 'A', 'B', 'B', 'A', 'C' }; //サンプルA
  103. BType B{ 'a','e','a','d','a','c','a','b' };//サンプルB
  104.  
  105. MakeHoge(A);
  106. std::cout << std::endl;
  107. MakeHoge(B);
  108. std::cout << std::endl;
  109. MakeHoge(D);
  110. std::cout << std::endl;
  111. return 0;
  112. }
Success #stdin #stdout 0s 3488KB
stdin
Standard input is empty
stdout
オリジナル
65,66,66,65,67,
復号化
65,66,66,65,67,
符号化@0
67,66,66,65,65,
データ完全一致!

オリジナル
97,101,97,100,97,99,97,98,
復号化
97,101,97,100,97,99,97,98,
符号化@3
99,100,101,98,97,97,97,97,
データ完全一致!

オリジナル
140,151,183,216,154,219,139,216,108,159,165,98,112,76,228,14,246,69,98,122,202,207,135,122,145,100,236,214,18,86,22,165,
復号化
86,22,165,140,151,183,216,154,219,139,216,108,159,165,98,112,76,228,14,246,69,98,122,202,207,135,122,145,100,236,214,18,
符号化@15
228,214,86,246,112,18,165,69,145,216,98,135,98,207,219,165,122,140,216,108,159,22,151,122,202,236,139,183,154,76,100,14,
データ完全一致しませんでした!