Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cassert>
- #include <iostream>
- #include <sstream>
- #include <unordered_map>
- #include <vector>
- using namespace std;
- vector<vector<string>> all_comb(const vector<string> &v, const size_t count)
- {
- assert(count <= v.size());
- vector<bool> bitset(v.size() - count, 0);
- bitset.resize(v.size(), 1);
- vector<vector<string>> result;
- do {
- vector<string> tmp;
- for (size_t i = 0; i != v.size(); ++i) {
- if (bitset[i])
- tmp.push_back(v[i]);
- }
- result.push_back(tmp);
- } while (next_permutation(bitset.begin(), bitset.end()));
- return result;
- }
- void get_all_stat(vector<vector<string>> &stat, const vector<string> &s, const vector<double> &d)
- {
- // add all comb without d
- for (size_t i = 1; i <= s.size(); i++)
- {
- vector<vector<string>> ret;
- ret = all_comb(s, i);
- stat.insert(stat.end(), ret.begin(), ret.end());
- }
- // generate list mixed s and d
- vector<string> tmp;
- for (const auto &i : s)
- {
- tmp.push_back(i);
- for (const auto &j : d)
- {
- ostringstream oss;
- oss << noshowpoint << j;
- string s = oss.str();
- tmp.push_back(i + ":" + s);
- }
- }
- // calculate all comb
- for (size_t i = 1; i <= tmp.size(); i++)
- {
- vector<vector<string>> ret;
- ret = all_comb(tmp, i);
- for (const auto &v : ret)
- {
- unordered_map<string, unsigned> seen_s;
- unordered_map<double, bool> seen_d;
- for (const auto &s : v)
- {
- string left { s };
- double right { 0 };
- string::size_type pos { s.find(":") };
- // on a trouvé un :
- if (pos != string::npos)
- {
- left = s.substr(0, pos);
- right = stod(s.substr(pos + 1));
- }
- seen_s[left] += 1;
- seen_d[right] = true;
- }
- // we dont want combination with all same "d"
- if (seen_d.size() <= 1)
- continue;
- // we dont want combination with more than one "s"
- bool add { true };
- for (const auto &m : seen_s)
- {
- if (m.second > 1)
- {
- add = false;
- break;
- }
- }
- if (add)
- stat.push_back(v);
- }
- }
- }
- int main()
- {
- vector<string> s { "a", "b" };
- vector<double> d { 2.5, 3.7 };
- vector<vector<string>> stat;
- get_all_stat(stat, s, d);
- for (const auto &i : stat)
- {
- for (const auto &j : i)
- cout << " " << j;
- cout << endl;
- }
- }
Add Comment
Please, Sign In to add comment