Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include "solution.h"
  2.  
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <unordered_set>
  7.  
  8. struct HistoryRecord {
  9.     int value;
  10.     int position;
  11. };
  12.  
  13. std::vector<int> CreateStartPermutation(size_t alphabet_size) {
  14.     std::vector<int> start_permutation(alphabet_size + 1);
  15.     for (size_t i = 1; i < start_permutation.size(); ++i) {
  16.         start_permutation[i] = i;
  17.     }
  18.     return start_permutation;
  19. }
  20.  
  21. std::vector<int> ReversedPermutation(const std::vector<int>& permutation) {
  22.     std::vector<int> reversed_permutation(permutation.size());
  23.     for (size_t i = 1; i < permutation.size(); ++i) {
  24.         reversed_permutation[permutation[i]] = i;
  25.     }
  26.     return reversed_permutation;
  27. }
  28.  
  29. std::vector<int> ApplyHistory(
  30.         const std::vector<int>& permutation,
  31.         const std::vector<HistoryRecord>& history
  32. ) {
  33.     std::vector<int> updated_permutation(permutation.size());
  34.     std::unordered_set<int> unique;
  35.     auto updated_it = updated_permutation.begin();
  36.     for (int i = history.size() - 1; i >= 0; --i) {
  37.         if (unique.find(history[i].value) == unique.end()) {
  38.             *updated_it = history[i].value;
  39.             ++updated_it;
  40.             unique.insert(history[i].value);
  41.         }
  42.     }
  43.     for (int value : permutation) {
  44.         if (unique.find(value) == unique.end()) {
  45.             *updated_it = value;
  46.             ++updated_it;
  47.         }
  48.     }
  49.  
  50.     return updated_permutation;
  51. }
  52.  
  53. int FindPosByValue(
  54.         int value,
  55.         const std::vector<int>& permutation,
  56.         const std::vector<int>& reversed_permutation,
  57.         const std::vector<HistoryRecord>& history
  58. ) {
  59.     for (int i = history.size() - 1; i >= 0; ++i) {
  60.         if (value == history[i].value) {
  61.             return i;
  62.         }
  63.     }
  64.  
  65.  
  66. }
  67.  
  68. void ProcessBlock(
  69.         const std::vector<int>& source_text,
  70.         std::vector<int>& result_text,
  71.         const std::vector<int>& permutation,
  72.         std::vector<HistoryRecord>& history,
  73.         int type,
  74.         int block_begin,
  75.         int block_end
  76. ) {
  77.     const std::vector<int> reversed_permutation = ReversedPermutation(permutation);
  78.     for (int i = block_begin; i < block_end; ++i) {
  79.        
  80.     }
  81.     if (type == 1) {
  82.  
  83.     } else {
  84.  
  85.     }
  86.  
  87. }
  88.  
  89. std::vector<int> Solution(
  90.         const std::vector<int>& source_text,
  91.         size_t alphabet_size,
  92.         int type
  93. ) {
  94.     const int BLOCK_SIZE = static_cast<int>(std::sqrt(static_cast<double>(source_text.size()))) + 1;
  95.     const int BLOCKS_COUNT = (static_cast<int>(source_text.size()) + BLOCK_SIZE - 1) / BLOCK_SIZE;
  96.     std::vector<int> result_text(source_text.size());
  97.  
  98.     std::vector<int> permutation = CreateStartPermutation(alphabet_size);
  99.     std::vector<HistoryRecord> history;
  100.     history.reserve(BLOCK_SIZE);
  101.     for (int block_id = 0; block_id < BLOCKS_COUNT; ++block_id) {
  102.         const int BLOCK_BEGIN_ID = block_id * BLOCK_SIZE;
  103.         const int BLOCK_END_ID = std::min<int>(BLOCK_BEGIN_ID + BLOCK_SIZE, source_text.size());
  104.         permutation = ApplyHistory(permutation, history);
  105.         ProcessBlock(
  106.                 source_text,
  107.                 result_text,
  108.                 permutation,
  109.                 history,
  110.                 type,
  111.                 BLOCK_BEGIN_ID,
  112.                 BLOCK_END_ID
  113.         );
  114.     }
  115.  
  116.     return result_text;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement