集合(normal)
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!
.
.
.
.
.
分析
我們先不管A 與 B 的具體關系如何,注意到這個問題的本質就是對于給定的集合A ,確定B 中的元素是否在 A 中。所以,我們使用哈希表來處理。至于哈希函數,只要按照除余法就行了
當然,也可以用快排+二分做
.
.
.
.
.
哈希
.
.
.
.
.
快排+二分
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int ans=0,a[100001],na,nb;void ef(int x) {int l=1,r=na,mid;while (l<=r){mid=(l+r)/2;if (a[mid]==x) {ans++;return;}if (a[mid]>x) r=mid-1; else l=mid+1;} }int main() {scanf("%d",&na);for (int i=1;i<=na;i++)scanf("%d",&a[i]);sort(a+1,a+na+1);scanf("%d",&nb);for (int i=1;i<=nb;i++){int b;scanf("%d",&b);ef(b);}if (na==nb&&na==ans) printf("A equals B"); elseif (ans==0) printf("A and B are disjoint"); elseif (na>nb&&nb==ans) printf("B is a proper subset of A"); elseif (nb>na&&na==ans) printf("A is a proper subset of B"); elseprintf("I'm confused!");return 0; }```轉載于:https://www.cnblogs.com/YYC-0304/p/10292794.html
總結
以上是生活随笔為你收集整理的集合(normal)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Treasure Exploration
- 下一篇: Magic Squares 魔板 (BF