#include <iostream> #include <vector> #include <algorithm> #include <cstdint> #include <random> #include <limits> #include <functional> #include <map> typedef std::vector<std::uint8_t> BType; typedef std::vector<BType> Block; //have bug! BType MakeData(std::size_t N = std::numeric_limits<std::uint16_t>::max()){ std::random_device RD; // std::mt19937 mt(RD()); std::mt19937 mt(0); std::uniform_int_distribution<int> uid(0, 255); BType ret; for (std::size_t i = 0; i < N; i++){ ret.push_back(uid(mt)); } return ret; } bool Append(Block& B, BType& Data){ B.push_back(Data); return true; } std::pair<std::size_t,BType> BlockSortEn(BType& Data){ auto T = Data; Block B; BType R; std::size_t j = 0; for (std::size_t i = 1; i <= T.size(); i++){ Append(B, T); std::rotate(T.begin(), T.begin()+1, T.end()); } std::sort(B.begin(), B.end()); for (std::size_t i = 0; i < B.size(); i++){ R.push_back(B[i][Data.size() - 1]); if (B[i] == Data) j = i; } return std::make_pair(j,R); } BType BlockSortDe(std::pair<std::size_t, BType>& Data){ std::vector<std::pair<std::uint8_t,std::size_t>> T; BType R; for (std::size_t i = 0; i < Data.second.size(); i++){ T.push_back(std::make_pair(Data.second[i], i)); } std::sort(T.begin(), T.end()); std::size_t P = 0; for (std::size_t i = 0; i < T.size(); i++){ R.push_back(T[P].first); P = T[P].second; } if(Data.first !=0) std::rotate(R.begin(), R.begin() + Data.first-1 , R.end()); return R; } bool MakeHoge(BType D){ auto BSE = BlockSortEn(D); auto BSD = BlockSortDe(BSE); std::cout << "オリジナル" << std::endl; for (auto& o : D) std::cout << static_cast<int>(o) << ','; std::cout << std::endl; std::cout << "復号化" << std::endl; for (auto& o : BSD) std::cout << static_cast<int>(o) << ','; std::cout << std::endl; std::cout << "符号化@" <<BSE.first<< std::endl; for (auto& o : BSE.second) std::cout << static_cast<int>(o) << ','; std::cout << std::endl; if (D == BSD){ std::cout << "データ完全一致!" << std::endl; } else{ std::cout << "データ完全一致しませんでした!" << std::endl; } return true; } int main(){ static const std::size_t L = 32; auto D = MakeData(L); BType A{ 'A', 'B', 'B', 'A', 'C' }; //サンプルA BType B{ 'a','e','a','d','a','c','a','b' };//サンプルB MakeHoge(A); std::cout << std::endl; MakeHoge(B); std::cout << std::endl; MakeHoge(D); std::cout << std::endl; return 0; }
Standard input is empty
オリジナル 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, データ完全一致しませんでした!