JZOJ【hash】集合的關係 (set)

題目描述

給定兩個集合A、B,集合內的任一元素x滿足 1x1091 ≤ x ≤ 10^9 ,並且每個集合的元素個數不大於 10510^ 5。我們希望求出A、B之間的關係。

給定兩個集合的描述,判斷它們滿足下列關係的哪一種:

A是B的一個真子集,輸出“AisapropersubsetofBA is a proper subset of B

B是A的一個真子集,輸出“BisapropersubsetofAB is a proper subset of A

A和B是同一個集合,輸出“AequalsBA equals B

A和B的交集爲空,輸出“AandBaredisjointA and B are disjoint

上述情況都不是,輸出“Imconfused!I'm confused!

輸入格式

有兩行,分別表示兩個集合,每行的第一個整數爲這個集合的元素個數(至少一個),然後緊跟着這個集合的元素(均爲不同的正整數)

輸出格式

只有一行,就是A、B的關係。

解題思路

  • 其實呢,這道題就是一道玩 setset 的板子題,爲什麼這麼說呢,雖然題目標籤上寫着 hashhash,你根本就可以不去管,思路很簡單,兩個並列的循環,用 numanuma 統計 B 中和 A 相同的元素個數,用 numbnumb 統計 A 中和 B 相同的元素個數,然後就是純粹的 ifif 判斷了。

Code

#include <bits/stdc++.h>
using namespace std;
int n,m;
set<int> a;
set<int> b;
void Fre()
{
	freopen("subset.in","r",stdin);
	freopen("subset.out","w",stdout);
}
void Input()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		a.insert(x);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		int x;
		scanf("%d",&x);
		b.insert(x);
	}
}
void work()
{
	int numa=0,numb=0;
	for (set<int>::iterator it=a.begin();it!=a.end();it++)
	{
		if (b.count(*it))  //統計
		{
			numa++;
		}
	}
	for (set<int>::iterator it=b.begin();it!=b.end();it++)
	{
		if (a.count(*it))  //統計
		{
			numb++;
		}
	}
	if (n<m && numa==n) printf("A is a proper subset of B");  //A中的所有元素B中都有且B的長度大於A
	else
	if (n>m && numb==m) printf("B is a proper subset of A");  //B中的所有元素A中都有且A的長度大於B
	else
	if (n==m && numa==n && numb==m) printf("A equals B");  //兩個集合完全相同
	else
	if (numa==0 && numb==0) printf("A and B are disjoint");  //A中的所有元素B中都沒有且B中的所有元素A中都沒有
	else printf("I'm confused!");
}
int main()
{
	Fre();
	Input();
	work();
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章