Advertisement
TimSenin

Untitled

Nov 19th, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <vector>
  4. #include <cstddef>
  5. #include <cstdint>
  6. #include <string>
  7. #include <set>
  8. #include <map>
  9.  
  10. struct Subsets {
  11.     void BuildMasksForLeftPart(const std::vector<int64_t>& vector, size_t from, size_t to, std::string mask, int border);
  12.  
  13.     void BuildMasksForRightPart(const std::vector<int64_t>& vector, size_t from, size_t to, std::string mask, int border);
  14.  
  15.     int64_t SumOfMask(const std::vector<int64_t>& vector, size_t from, size_t to, std::string mask);
  16.  
  17.     std::vector<size_t> first_indices;
  18.     std::vector<size_t> second_indices;
  19.     bool exists = false;
  20.  
  21.     std::set<char> symbols = {'0', '1', '2'};
  22.     std::map<int64_t, std::string> left_sums;
  23. };
  24.  
  25. Subsets FindEqualSumSubsets(const std::vector<int64_t>& data);
  26.  
  27.  
  28.  
  29. #include "find_subsets.h"
  30. #include <vector>
  31.  
  32. #include <iostream>
  33.  
  34. Subsets FindEqualSumSubsets(const std::vector<int64_t>& data) {
  35.     Subsets subsets;
  36.     int border = data.size() / 3;
  37.     subsets.BuildMasksForLeftPart(data, 0, border, "", border);
  38.     for (auto& [k, v] : subsets.left_sums) {
  39.         std::cout << k << ' ' << v << std::endl;
  40.     }
  41. //    subsets.BuildMasksForRightPart(data, border + 1, data.size() - border, "", data.size());
  42.     return subsets;
  43. }
  44.  
  45. //void Subsets::BuildMasksForRightPart(const std::vector<int64_t>& vector, size_t from, size_t to, std::string mask, int border) {
  46. //    if (exists) {
  47. //        return;
  48. //    }
  49. //    if (mask.size() == border) {
  50. //        if (left_sums.contains(SumOfMask(vector, from, to, mask))) {
  51. //            exists = true;
  52. //            return;
  53. //        }
  54. //        return;
  55. //    }
  56. //    for (char elem : symbols) {
  57. //        mask.push_back(elem);
  58. //        BuildMasksForLeftPart(vector, from, to, mask, border);
  59. //        mask.pop_back();
  60. //    }
  61. //}
  62.  
  63. // TODO наверное стоит переделать через классы
  64. void Subsets::BuildMasksForLeftPart(const std::vector<int64_t>& vector, size_t from, size_t to, std::string mask, int border) {
  65.     if (mask.size() == border) {
  66.         left_sums.insert({SumOfMask(vector, from, to, mask), mask});
  67.         return;
  68.     }
  69.     for (char elem : symbols) {
  70.         mask.push_back(elem);
  71.         BuildMasksForLeftPart(vector, from, to, mask, border);
  72.         mask.pop_back();
  73.     }
  74. }
  75. int64_t Subsets::SumOfMask(const std::vector<int64_t>& vector, size_t from, size_t to, std::string mask) {
  76.     int64_t result = 0;
  77.     for (size_t i = from; i < to; ++i) {
  78.         if (mask[i] == 1) {
  79.             result += vector[i];
  80.         } else if (mask[i] == 2) {
  81.             result -= vector[i];
  82.         }
  83.     }
  84.     return result;
  85. }
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement