离散题目8

来源:互联网 发布:莎士比亚别生气 知乎 编辑:程序博客网 时间:2024/05/29 21:28

离散题目8

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

现有一个全集U,U={ x | x>=1 && x<=N } 。

对于U的任意子集A,现在定义一种位集(bitset)Abit用来描述U的子集A: 该位集由1,0组成,长度为N,对于集合A中的任意元素x,集合Abit 在第x位且仅在第x位有对应的1存在,其余位置为0。

例如:     对于全集U,其对应的描述位集Ubit = { 111...1 } (N个1);     对于集合A = { 1,2,3,N },其对应的描述位集Abit   = { 1110...01 };

Input

多组输入,每组输入包括三行,第一行为集合U的指标参数N( 0< N < = 64 ),第二行为集合A的元素,第三行为集合B的元素,元素之间用空格分割,具体参考示例输入。

Output

每组输入对应两行输出,第一行为A、B的交集的描述位集。第二行为A、B的并集的描述位集。

Example Input

101 3 5 7 82 5 6

Example Output

00001000001110111100

Hint

Author


#include <iostream>#include <bits/stdc++.h>using namespace std;int main(){    int n;    while(cin>>n)    {        set<int>a,b;        set<int>::iterator it;        string st;        string sr;        string buf;        getline(cin,buf);//因为两个getline连用的换第二个会覆盖掉第一个,所以前面加一行无用的getline(cin>>buf)做铺垫;        getline(cin,st);//输入两个字符串        stringstream ss(st);//拆开字符串        getline(cin,sr);        while(ss >> buf)        {            int t;            sscanf(buf.c_str(), "%d", &t);            a.insert(t);//存入集合中        }        stringstream cc(sr);        while(cc>>buf)        {            int t;            sscanf(buf.c_str(), "%d", &t);            b.insert(t);        }        int bb[100] = {0};        int dd[100] = {0};        for(it= a.begin(); it != a.end(); it++)//求交集        {            if(b.find(*it) != b.end())                bb[*it - 1] = 1;//存在即该元素位置即为1        }        for(it = b.begin(); it != b.end(); it++)//合并a,b两个集合            a.insert(*it);        for(int i = 0; i < n; i++)        {            cout<<bb[i];        }        cout<<endl;        for(it = a.begin(); it != a.end(); it++)//在并集中存在元素,该元素位置即为1;        {            dd[*it -1] = 1;        }        for(int i = 0; i < n; i++)        {            cout<<dd[i];        }        cout<<endl;    }    return 0;}