Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include "find_subsets.h"
  2. #include <stdexcept>
  3. #include <vector>
  4. #include <unordered_map>
  5. #include <cmath>
  6. #include <thread>
  7. #include <mutex>
  8.  
  9. #include <iostream>
  10.  
  11. std::unordered_map<int64_t, size_t> FindSum(std::unordered_map<int64_t, size_t>& mp, size_t i,
  12.                                             size_t j, const std::vector<int64_t>& data) {
  13.     if (i == j) {
  14.         return mp;
  15.     }
  16.     int n = j - i;
  17.     uint64_t bound = pow(3, n);
  18.     for (uint64_t k = 1; k < bound; ++k) {
  19.         int64_t sum = 0;
  20.         uint64_t kk = k;
  21.         int ind = i;
  22.         while (kk != 0) {
  23.             if (kk % 3 == 2) {
  24.                 sum += data[ind];
  25.             } else if (kk % 3 == 1) {
  26.                 sum -= data[ind];
  27.             }
  28.             ind++;
  29.             kk = kk / 3;
  30.         }
  31.         mp[sum] = k;
  32.     }
  33.     return mp;
  34. }
  35.  
  36. void FindIndexes(size_t num, std::vector<size_t>& first, std::vector<size_t>& second, int i) {
  37.     int ind = i;
  38.     while (num != 0) {
  39.         if (num % 3 == 2) {
  40.             first.push_back(ind);
  41.         } else if (num % 3 == 1) {
  42.             second.push_back(ind);
  43.         }
  44.         ind++;
  45.         num /= 3;
  46.     }
  47. }
  48.  
  49. Subsets subset;
  50. bool fl;
  51. std::mutex mtx;
  52.  
  53. void Result(const std::unordered_map<int64_t, size_t>& mp1,
  54.             const std::unordered_map<int64_t, size_t>& mp2,
  55.             size_t sz, int i) {
  56.     for (const auto& it = std::next(mp1.begin(), i); it != mp1.end() && !fl; std::next(it, 4)) {
  57.         const auto& key = (*it).first;
  58.         if (mp2.find(key) != mp2.end()) {
  59.                 std::cout << "DDD" << std::endl;
  60.             std::vector<size_t> first, second;
  61.             std::cout << "A4" << std::endl;
  62.             FindIndexes(mp1.at(key), first, second, 0);
  63.             FindIndexes(mp2.at(key), second, first, sz);
  64.             std::cout << "A5" << std::endl;
  65.             if (!first.empty() && !second.empty()) {
  66.                 mtx.lock();
  67.                 fl = true;
  68.                 subset = Subsets{first, second, true};
  69.                 mtx.unlock();
  70.                 return;
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. Subsets FindEqual(const std::unordered_map<int64_t, size_t>& mp1,
  77.                    const std::unordered_map<int64_t, size_t>& mp2, size_t sz) {
  78.     fl = false;
  79.         std::cout << "A2" << std::endl;
  80.     std::thread t1([&mp1, &mp2, sz] { Result(mp1, mp2, sz, 0); });
  81.     std::thread t2([&mp1, &mp2, sz] { Result(mp1, mp2, sz, 1); });
  82.     std::thread t3([&mp1, &mp2, sz] { Result(mp1, mp2, sz, 2); });
  83.     std::thread t4([&mp1, &mp2, sz] { Result(mp1, mp2, sz, 3); });
  84.     t1.join();
  85.     t2.join();
  86.     t3.join();
  87.     t4.join();
  88.         std::cout << "A3" << std::endl;
  89.  
  90.     if (fl) {
  91.         return subset;
  92.     }
  93.     return Subsets{{}, {}, false};
  94. }
  95.  
  96. Subsets FindEqualSumSubsets(const std::vector<int64_t>& data) {
  97.     size_t sz = data.size() / 2;
  98.     std::unordered_map<int64_t, size_t> mp1;
  99.     std::unordered_map<int64_t, size_t> mp2;
  100.     std::thread th1([&mp1, sz, &data] { FindSum(mp1, 0, sz, data); });
  101.     std::thread th2([&mp2, sz, &data] { FindSum(mp2, sz, data.size(), data); });
  102.     th1.join();
  103.     th2.join();
  104.     std::vector<size_t> first, second;
  105.     if (mp1.find(0) != mp1.end()) {
  106.         FindIndexes(mp1[0], first, second, 0);
  107.         return Subsets{first, second, true};
  108.     }
  109.     if (mp2.find(0) != mp2.end()) {
  110.         FindIndexes(mp2[0], first, second, sz);
  111.         return Subsets{first, second, true};
  112.     }
  113.     std::cout << "A1" << std::endl;
  114.     return FindEqual(mp1, mp2, sz);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement