Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 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> ApplyHistory(
  22.         const std::vector<int>& permutation,
  23.         const std::vector<HistoryRecord>& history
  24. ) {
  25.     std::vector<int> updated_permutation(permutation.size());
  26.     std::unordered_set<int> unique;
  27.     auto updated_it = updated_permutation.begin();
  28.     for (int i = history.size() - 1; i >= 0; --i) {
  29.         if (unique.find(history[i].value) == unique.end()) {
  30.             *updated_it = history[i].value;
  31.             ++updated_it;
  32.             unique.insert(history[i].value);
  33.         }
  34.     }
  35.     for (int value : permutation) {
  36.         if (unique.find(value) == unique.end()) {
  37.             *updated_it = value;
  38.             ++updated_it;
  39.         }
  40.     }
  41.  
  42.     return updated_permutation;
  43. }
  44.  
  45. void ProcessBlock(
  46.         const std::vector<int>& source_text,
  47.         )
  48.  
  49. std::vector<int> Solution(
  50.         const std::vector<int>& source_text,
  51.         size_t alphabet_size,
  52.         int type
  53. ) {
  54.     const int BLOCK_SIZE = static_cast<int>(std::sqrt(static_cast<double>(source_text.size()))) + 1;
  55.     const int BLOCKS_COUNT = (static_cast<int>(source_text.size()) + BLOCK_SIZE - 1) / BLOCK_SIZE;
  56.     std::vector<int> result_text(source_text.size());
  57.  
  58.     std::vector<int> permutation = CreateStartPermutation(alphabet_size);
  59.     std::vector<HistoryRecord> history;
  60.     history.reserve(BLOCK_SIZE);
  61.     for (int block_id = 0; block_id < BLOCKS_COUNT; ++block_id) {
  62.         const int BLOCK_BEGIN_ID = block_id * BLOCK_SIZE;
  63.         const int BLOCK_END_ID = std::min<int>(BLOCK_BEGIN_ID + BLOCK_SIZE, source_text.size());
  64.         permutation = ApplyHistory(permutation, history);
  65.         ProcessBlock(
  66.                 source_text,
  67.                 result_text,
  68.                 permutation,
  69.                 history,
  70.                 type,
  71.                 BLOCK_BEGIN_ID,
  72.                 BLOCK_END_ID
  73.         );
  74.     }
  75.  
  76.     return result_text;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement