C++ n选择带条件的k实现
我在本主题中使用了代码 我对它进行了一些编辑,以处理C++ n选择带条件的k实现,c++,C++,我在本主题中使用了代码 我对它进行了一些编辑,以处理std::vector而不是std::vector 在我的例子中,K有不同的值(从1到4),而只有一个值 示例:我的数据 1 2 3 4 1 2 4 1 3 如果k=3,那么对于我的数据中的每一行,n选择k= 123 124 134 234 124 13 但我希望在map中得到结果,其中的值表示子集的频率: (123, 1) (124, 2) (134, 1) (234, 1) (13, 1) 这是我的代码: std::vecto
std::vector
而不是std::vector
在我的例子中,K有不同的值(从1到4),而只有一个值
示例:我的数据
1 2 3 4
1 2 4
1 3
如果k=3,那么对于我的数据中的每一行,n选择k=
123
124
134
234
124
13
但我希望在map中得到结果,其中的值表示子集的频率:
(123, 1)
(124, 2)
(134, 1)
(234, 1)
(13, 1)
这是我的代码:
std::vector<std::vector<int>> data;
std::map <int, set<int>> CandidateSet;
typedef std::pair<set<int>, int> combo;
std::map <int, combo> CandidateSup;
void ScanData()
{
ifstream in;
in.open("mydata.txt");
/* mydata.txt
1 2 3 4 5
1 3 4 5
1 2 3 5
1 3
*/
std::string line;
int i = 0;
while (std::getline(in, line))
{
std::stringstream Sline1(line);
std::stringstream ss(line);
std::vector<int> inner;
int info;
while (ss >> info)
inner.push_back(info);
data.push_back(inner);
}
}
int main()
{
ScanData();
std::size_t k = 0;
int j = 0;
int c = 0;
int supp = 1;
int Lsize = 1;
while (Lsize <= 4)
{
for (unsigned i = 0; i < data.size(); ++i)
{
std::vector<int>::iterator items = data[i].begin();
if (Lsize > data[i].size())
k = data[i].size();
else
k = Lsize;
do
{
for (std::vector<int>::iterator items = data[i].begin(); j < k; ++items)
{
CandidateSet[c].insert(*items);
++j;
}
++c;
std::cout << "\n";
j = 0;
}
while (next_combination(data[i].begin(), data[i].begin() + k, data[i].end()));
/******************************************************************************************************************************
//here my problem
// check if the (next_combination) is already exist in CandidateSet, if yes add one to existing support
auto it = CandidateSet.begin();
set <int> A = it->second;
set <int> B;
B = CandidateSet[*items]; // I don't know how to set be as the next_combination
while (it != CandidateSet.end())
{ // if it found
if (!(A < B) && !(B < A))
{
CandidateSup[*items] = std::make_pair(A, ++supp);
break;
}
else
{ // if not found yet
++it;
A = it->second;
}
}// end while
// if it is not exist, add this new set with support =1
++c;
CandidateSup[c] = std::make_pair(B, supp);
/******************************************************************************************************************************/
}
++Lsize;
}
data.clear();
system("PAUSE");
return 0;
}
std::矢量数据;
地图候选集;
typedef std::pair组合;
标准::映射候选项UP;
无效Scanda()
{
如果输入;
in.open(“mydata.txt”);
/*mydata.txt
1 2 3 4 5
1 3 4 5
1 2 3 5
1 3
*/
std::字符串行;
int i=0;
while(std::getline(in,line))
{
标准::stringstream Sline1(线);
std::stringstream ss(线路);
std::向量内部;
国际信息;
而(ss>>信息)
内部。推回(信息);
数据。推回(内部);
}
}
int main()
{
斯堪的纳维亚();
标准:尺寸k=0;
int j=0;
int c=0;
int supp=1;
int-Lsize=1;
while(Lsize data[i].size())
k=数据[i]。大小();
其他的
k=Lsize;
做
{
对于(std::vector::iterator items=data[i].begin();j秒;
}
}//结束时
//如果不存在,请添加此新集合,并使用support=1
++c;
CandidateSup[c]=std::make_pair(B,supp);
/******************************************************************************************************************************/
}
++Lsize;
}
data.clear();
系统(“暂停”);
返回0;
}
此“组合.h”
模板
内联bool next_组合(常量迭代器优先、迭代器k、常量迭代器最后)
{
/*字幕:托马斯·德雷珀*/
if((first==last)| |(first==k)| |(last==k))
返回false;
迭代器itr1=第一个;
迭代器itr2=最后一个;
++itr1;
if(last==itr1)
返回false;
itr1=最后一个;
--itr1;
itr1=k;
--itr2;
while(第一个!=itr1)
{
如果(*-itr1<*itr2)
{
迭代器j=k;
而(!(*itr1<*j))++j;
标准:国际热核实验堆交换(itr1,j);
++itr1;
++j;
itr2=k;
标准:旋转(itr1,j,最后);
while(last!=j)
{
++j;
++itr2;
}
标准:旋转(k,itr2,最后);
返回true;
}
}
标准::旋转(第一,k,最后);
返回false;
}
以下内容可能会有所帮助:()
std::map计数;
对于(std::size_t Lsize=1;Lsize问题是什么?与其只打印n选择k,不如检查下一个组合是否与上一个组合相同,如果是,则在该映射的值中添加一个候选项up,如果不是,则设置值=1,请参见示例,***之间的代码是我的尝试。我不知道如何感谢你,我在这一点上停留了很长时间,一个d厌倦了编写许多代码来解决这个问题。多亏了洛蒂又增加了一个条件来生成更多的超集:用户输入特定的阈值,外部循环从1开始到数据的最大大小[i].在生成第一个集合后,我擦除阈值以下的每个集合,我将擦除的集合存储在Ls中,然后在外部循环的第二轮中,我检查n_k是否在Ls中,然后无需生成此集合,我将跳转生成下一个n_k程序工作,但我有很多循环,我不知道是否有有效的方法来最小化运行时间。我希望你能看看更新,并帮助我的想法。
template <typename Iterator>
inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
{
/* Credits: Thomas Draper */
if ((first == last) || (first == k) || (last == k))
return false;
Iterator itr1 = first;
Iterator itr2 = last;
++itr1;
if (last == itr1)
return false;
itr1 = last;
--itr1;
itr1 = k;
--itr2;
while (first != itr1)
{
if (*--itr1 < *itr2)
{
Iterator j = k;
while (!(*itr1 < *j)) ++j;
std::iter_swap(itr1, j);
++itr1;
++j;
itr2 = k;
std::rotate(itr1, j, last);
while (last != j)
{
++j;
++itr2;
}
std::rotate(k, itr2, last);
return true;
}
}
std::rotate(first, k, last);
return false;
}
std::map<std::set<int>, int> counts;
for (std::size_t Lsize = 1; Lsize <= 4; ++Lsize)
{
for (unsigned i = 0; i < data.size(); ++i)
{
std::size_t k = std::min(Lsize, data[i].size());
do
{
std::set<int> n_k(data[i].begin(), data[i].begin() + k);
++counts[n_k];
}
while (next_combination(data[i].begin(), data[i].begin() + k, data[i].end()));
}
}