set_union的几个例子

本文介绍了一种将多个含有交集的集合合并为无交集集合的方法。通过使用C++标准库中的set_union函数实现集合合并,并提供了三个不同场景下的示例代码。这些示例展示了如何处理数组、vector和set类型的数据结构。

问题:将多个集合合并成没有交集的集合。
  给定一个字符串的集合,格式如:{aaabbbccc},{bbbddd},{eeefff},{ggg},{dddhhh}要求将其中交集不为空的集合合并,要求合并完成后的集合之间无交集,例如上例应输出{aaabbbcccdddhhh},{eeefff},{ggg}。
  (1)请描述你解决这个问题的思路;
  (2)请给出主要的处理流程,算法,以及算法的复杂度
  (3)请描述可能的改进。

考虑使用泛型算法set_union( ).

OutputIterator set_union (InputIterator source1Beg, InputIterator source1End, InputIterator source2Beg, InputIterator source2End, OutputIterator destBeg)

Return value: An iterator to the end of the constructed range.

例子:

http://www.cplusplus.com/reference/algorithm/set_union/

// set_union example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
  int first[] = {5,10,15,20,25};
  int second[] = {50,40,30,20,10};
  vector<int> v(10);                           // 0  0  0  0  0  0  0  0  0  0
  vector<int>::iterator it;

  sort (first,first+5);     //  5 10 15 20 25
  sort (second,second+5);   // 10 20 30 40 50

  it=set_union (first, first+5, second, second+5, v.begin());
                                               // 5 10 15 20 25 30 40 50  0  0
  cout << "union has " << int(it - v.begin()) << " elements.\n";
  return 0;
}


// set_union2 example
#include <set>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;

int main(void)
{
    set<int> a,b,c;
    a.insert(1);
    a.insert(6);
    a.insert(6);
    b.insert(2);
    b.insert(6);
    b.insert(9);

    //最后一个参数若使用c.begin()会产生编译错误assignment of read-only localtion.

    set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
    copy(c.begin(), c.end(), ostream_iterator <int> (cout, " "));
    return 0;
} 


// set_union3 example
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    vector<int> a,b,c;
    for(int e=0;e<10;e++)
    {
       a.push_back(e);
       b.push_back(e+5);
    }
    //最后一个参数若使用c.begin(),运行时会出错“Segmentation fault (core dumped)”.
    set_union(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
    copy(c.begin(), c.end(), ostream_iterator<int> (cout, " "));
    return 0;
}


最小 σ - 代数,也称为由一个集合类生成的 σ - 代数,是包含给定集合类的最小的 σ - 代数。以下是几个最小 σ - 代数的例子: ### 例子 1:单元素集合 设 \(X=\{a,b,c\}\),考虑集合类 \(\mathcal{C}=\{\{a\}\}\)。由 \(\mathcal{C}\) 生成的最小 σ - 代数 \(\sigma(\mathcal{C})\) 是包含 \(\{a\}\) 并且对补运算和可数并运算封闭的最小集合族。 \(\sigma(\mathcal{C})=\{\varnothing,\{a\},\{b,c\},X\}\) 验证: - 空集 \(\varnothing\) 必须在 \(\sigma(\mathcal{C})\) 中,因为 σ - 代数的定义要求。 - \(\{a\}\) 是给定的集合。 - \(\{a\}\) 的补集是 \(\{b,c\}\),所以 \(\{b,c\}\) 也在 \(\sigma(\mathcal{C})\) 中。 - 整个集合 \(X\) 是 \(\varnothing\) 的补集,所以 \(X\) 也在 \(\sigma(\mathcal{C})\) 中。 ### 例子 2:两个不相交集合 设 \(X = \{1, 2, 3, 4\}\),集合类 \(\mathcal{C}=\{\{1, 2\},\{3\}\}\)。 首先,我们知道 \(\varnothing\) 和 \(X\) 必须在 \(\sigma(\mathcal{C})\) 中。 \(\{1, 2\}\) 的补集是 \(\{3, 4\}\),\(\{3\}\) 的补集是 \(\{1, 2, 4\}\)。 通过可数并运算,我们可以得到其他元素。 \(\sigma(\mathcal{C})=\{\varnothing,\{1, 2\},\{3\},\{4\},\{1, 2, 3\},\{1, 2, 4\},\{3, 4\},X\}\) ### 代码示例 以下是一个 Python 代码示例,用于生成由一个集合类生成的最小 σ - 代数: ```python from itertools import chain, combinations def powerset(iterable): s = list(iterable) return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) def is_sigma_algebra(candidate, X): # 检查是否包含空集和全集 if frozenset() not in candidate or frozenset(X) not in candidate: return False # 检查补运算 for subset in candidate: complement = frozenset(X) - subset if complement not in candidate: return False # 检查可数并运算(这里简化为有限并) for subset1 in candidate: for subset2 in candidate: union = subset1.union(subset2) if union not in candidate: return False return True def generate_smallest_sigma_algebra(C, X): all_subsets = set(powerset(X)) for size in range(len(C), len(all_subsets)+1): for candidate in combinations(all_subsets, size): candidate = set(frozenset(subset) for subset in candidate) if all(frozenset(subset) in candidate for subset in C) and is_sigma_algebra(candidate, X): return candidate return None # 例子 X = {1, 2, 3, 4} C = {{1, 2}, {3}} sigma_algebra = generate_smallest_sigma_algebra(C, X) print(sigma_algebra) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值