离散题目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
10
1 3 5 7 8
2 5 6
Example Output
0000100000
1110111100
think:
按照题目的描述,根据题意,写了好几遍,也改了好几遍,就是不明白为什么WA……
后来,问了一个同学(SJ)他只加了一行代码,就AC了……
终于明白了离散的后台数据有多坑……
由于数据是在windows系统下生成的,换行并不是轻易的换行就能完成的,据说是\r\n, 要用两个getchar();
(他是这样解释的,我不知道对不对……)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
getchar();
getchar();//坑点
vector<int> a, b;
vector<int>::iterator it, it1;
int t;
string ac, bc, buf;
getline(cin, ac);
getline(cin, bc);
stringstream ss(ac);
while(ss>>buf)
{
sscanf(buf.c_str(), "%d", &t);
a.push_back(t);
}
stringstream cc(bc);
while(cc>>buf)
{
sscanf(buf.c_str(), "%d", &t);
b.push_back(t);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for(int i=1;i<=n;i++)
{
it = find(b.begin(), b.end(), i);
it1 = find(a.begin(), a.end(), i);
if((it!=b.end()))
{
if(it1!=a.end())
cout<<'1';
else
cout<<'0';
}
else
cout<<'0';
}
cout<<endl;
for(int i=1;i<=n;i++)
{
it = find(b.begin(), b.end(), i);
it1 = find(a.begin(), a.end(), i);
if((it!=b.end()))
{
cout<<'1';
}
else if(it1!=a.end())
{
cout<<'1';
}
else
cout<<'0';
}
cout<<endl;
a.clear();
b.clear();
}
return 0;
}
本文解析了一道关于离散数学中子集与位集描述的问题,通过编程方式求解两个集合的交集与并集,并分享了解决特殊输入格式问题的经验。
871

被折叠的 条评论
为什么被折叠?



