日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

集合(normal)

發布時間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集合(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> using namespace std;int mo=155559; int h[155559];int hash(int x) {int wz=x%mo;int i=0;while (i<mo&&h[(wz+i)%mo]!=-1&&h[(wz+i)%mo]!=x) i++;return (wz+i)%mo; }int main() {int na,nb;scanf("%d",&na);memset(h,-1,sizeof(h));for (int i=1;i<=na;i++){int a;scanf("%d",&a);h[hash(a)]=a;}int ans=0;scanf("%d",&nb);for (int i=1;i<=nb;i++){int b;scanf("%d",&b);if (h[hash(b)]==b) ans++;}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; }

.
.
.
.
.

快排+二分

#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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。