Guest User

Untitled

a guest
Nov 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <unordered_map>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. vector<vector<string>> all_comb(const vector<string> &v, const size_t count)
  11. {
  12. assert(count <= v.size());
  13. vector<bool> bitset(v.size() - count, 0);
  14. bitset.resize(v.size(), 1);
  15.  
  16. vector<vector<string>> result;
  17.  
  18. do {
  19. vector<string> tmp;
  20. for (size_t i = 0; i != v.size(); ++i) {
  21. if (bitset[i])
  22. tmp.push_back(v[i]);
  23. }
  24. result.push_back(tmp);
  25. } while (next_permutation(bitset.begin(), bitset.end()));
  26.  
  27. return result;
  28. }
  29.  
  30. void get_all_stat(vector<vector<string>> &stat, const vector<string> &s, const vector<double> &d)
  31. {
  32. // add all comb without d
  33. for (size_t i = 1; i <= s.size(); i++)
  34. {
  35. vector<vector<string>> ret;
  36. ret = all_comb(s, i);
  37. stat.insert(stat.end(), ret.begin(), ret.end());
  38. }
  39.  
  40. // generate list mixed s and d
  41. vector<string> tmp;
  42. for (const auto &i : s)
  43. {
  44. tmp.push_back(i);
  45. for (const auto &j : d)
  46. {
  47. ostringstream oss;
  48. oss << noshowpoint << j;
  49. string s = oss.str();
  50. tmp.push_back(i + ":" + s);
  51. }
  52. }
  53.  
  54. // calculate all comb
  55. for (size_t i = 1; i <= tmp.size(); i++)
  56. {
  57. vector<vector<string>> ret;
  58. ret = all_comb(tmp, i);
  59.  
  60. for (const auto &v : ret)
  61. {
  62. unordered_map<string, unsigned> seen_s;
  63. unordered_map<double, bool> seen_d;
  64.  
  65. for (const auto &s : v)
  66. {
  67. string left { s };
  68. double right { 0 };
  69.  
  70. string::size_type pos { s.find(":") };
  71.  
  72. // on a trouvé un :
  73. if (pos != string::npos)
  74. {
  75. left = s.substr(0, pos);
  76. right = stod(s.substr(pos + 1));
  77. }
  78.  
  79. seen_s[left] += 1;
  80. seen_d[right] = true;
  81. }
  82.  
  83. // we dont want combination with all same "d"
  84. if (seen_d.size() <= 1)
  85. continue;
  86.  
  87. // we dont want combination with more than one "s"
  88. bool add { true };
  89. for (const auto &m : seen_s)
  90. {
  91. if (m.second > 1)
  92. {
  93. add = false;
  94. break;
  95. }
  96. }
  97.  
  98. if (add)
  99. stat.push_back(v);
  100. }
  101. }
  102. }
  103.  
  104. int main()
  105. {
  106. vector<string> s { "a", "b" };
  107. vector<double> d { 2.5, 3.7 };
  108.  
  109. vector<vector<string>> stat;
  110. get_all_stat(stat, s, d);
  111.  
  112. for (const auto &i : stat)
  113. {
  114. for (const auto &j : i)
  115. cout << " " << j;
  116. cout << endl;
  117. }
  118. }
Add Comment
Please, Sign In to add comment