Description
给定两个集合A、B,集合内的任一元素x满足1 ≤ x ≤ 109,并且每个集合的元素个数不大于105。我们希望求出A、B之间的关系。
任 务 :给定两个集合的描述,判断它们满足下列关系的哪一种:
A是B的一个真子集,输出“A is a proper subset of B”
B是A的一个真子集,输出“B is a proper subset of A”
A和B是同一个集合,输出“A equals B”
A和B的交集为空,输出“A and B are disjoint”
上述情况都不是,输出“I’m confused!”
Input
输入有两行,分别表示两个集合,每行的第一个整数为这个集合的元素个数(至少一个),然后紧跟着这个集合的元素(均为不同的正整数)
Output
只有一行,就是A、B的关系。
Sample Input
样例1
2 55 27
2 55 27
样例2
3 9 24 1995
2 9 24
样例3
3 1 2 3
4 1 2 3 4
样例4
3 1 2 3
3 4 5 6
样例5
2 1 2
2 2 3
Sample Output
样例1
A equals B
样例2
B is a proper subset of A
样例3
A is a proper subset of B
样例4
A and B are disjoint
样例5
I’m confused!
思路
哈希直接过
(map不香吗)
code:
#include<cstring>
#include<iostream>
#include<cctype>
#include <cstdio>
#include<algorithm>
#define myd 1000007
using namespace std;
int n,m,ans;
int a[myd+1];
inline int ip()
{
char c=getchar();
int ans=0;
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=ans*10+c-48,c=getchar();
return ans;
}
int f(int x)
{
int o=x%myd,i=0;
while (i<myd&&a[(o+i)]!=x&&a[(o+i)%myd]) i++;
return (o+i)%myd;
}
bool find(int x)
{
return a[f(x)]==x;
}
void cr(int x)
{
a[f(x)]=x;
return;
}
int main()
{
n=ip();
for (int i=1;i<=n;i++)
{
cr(ip());
}
m=ip();
for (int j=1;j<=m;j++)
{
if (find(ip())) ans++;
}
if (ans==m&&ans==n) puts("A equals B");
else if (ans==m&&ans<n) puts("B is a proper subset of A");
else if (ans==n) puts("A is a proper subset of B");
else if (!ans) puts("A and B are disjoint");
else puts("I'm confused!");
return 0;
}
显然我们也有2分+sort的方法:
#include<cstring>
#include<iostream>
#include<cctype>
#include <cstdio>
#include<algorithm>
using namespace std;
int n,m,ans,a[100001],b[100001];
inline int ip()
{
char c=getchar();
int ans=0;
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=ans*10+c-48,c=getchar();
return ans;
}
void ef(int aa)//汉语拼音不要见怪
{
int l,r,mid;
l=1;r=n;
while(l<=r)
{
mid=(l+r)>>1;
if(a[mid]==aa) {
ans++;return;}
if(a[mid]>aa) r=mid-1;
else if(a[mid]<aa) l=mid+1;
}
}
int main()
{
n=ip();
for (int i=1;i<=n;i++)
{
a[i]=ip();
}
sort(a+1,a+n+1);
m=ip();
for (int j=0;j<m;j++)
{
ef(ip());
}
if (ans==m&&ans==n) puts("A equals B");
else if (ans==m&&ans<n) puts("B is a proper subset of A");
else if (ans==n) puts("A is a proper subset of B");
else if (!ans) puts("A and B are disjoint");
else puts("I'm confused!");
return 0;
}