我需要制作以下算法:
(Pseudocode)
lsi = (set_1, set_2, ..., set_n) # list of sets.
answer = [] // vector of lists of sets
For all set_i in lsi:
if length(set_i) > 1:
for all x in set_i:
A = set_i.discard(x)
B = {x}
answer.append( (set_1, ..., set_{i-1}, A, B, set_{i+1}, ..., set_n) )
例如,让lsi =({1,2},{3},{4,5})。
然后ansver = [({1},{2},{3},{4,5}),({2},{1},{3},{4,5}),({1 ,2},{3},{4},{5}),({1,2},{3},{5},{4})]。
我正在努力(C ++),但我做不到。 我的代码:
list<set<int>> lsi {{1, 2}, {3}, {4, 5}};
// Algorithm
vector<list<set<int>>> answer;
for (list<set<int>>::iterator it_lsi = lsi.begin(); it_lsi != lsi.end(); it_lsi++)
{
for (set<int>::iterator it_si = (*it_lsi).begin(); it_si != (*it_lsi).end(); it_lsi++)
{
// set_i = *it_lsi,
// x = *it_si.
// Creating sets A and B.
set<int> A = *it_lsi;
A.erase(*it_si); // A = set_i.discard(x)
set<int> B;
B.insert(*it_si); // B = {x}
// Creating list which have these sets.
list<set<int>> new_lsi;
new_lsi.push_back(s1);
new_lsi.push_back(s2);
/*
And I don't know what should I do :-(
Object lsi must be immutable (because it connect with generator).
Splice transform it. Other ways I don't know.
*/
}
}
你可以帮帮我吗?
谢谢。
答案 0 :(得分:1)
list<set<int>> lsi {{1, 2}, {3}, {4, 5}};
vector<list<set<int>>> answer;
for (auto it_lsi = lsi.begin(); it_lsi != lsi.end(); ++it_lsi)
{
if (it_lsi->size() > 1)
for (int i : *it_lsi)
{
list<set<int>> res {lsi.begin(), it_lsi};
set<int> A = *it_lsi;
A.erase(i);
set<int> B {i};
res.push_back(A);
res.push_back(B);
res.insert(res.end(), next(it_lsi), lsi.end());
answer.push_back(res);
}
}
具有简单输出的可运行代码:http://ideone.com/y2x35Q