离散题目3
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
DaYu在新的学习开始学习新的数学知识,一天DaYu学习集合的时候遇到一个问题,他有两个集合A和B,他想知道A是不是B的子集。
Input
多组输入,每组的第一行有两个数n,m,0 < n,m < 10^5。表示集合A的大小和集合B的大小。第二行输入n个数表示集合A,第三行输入m个数表示集合B,|data_i| < 10^5
Output
如果A是B的子集,输出"true",否则输出"false"。
Example Input
3 5 1 2 3 1 5 4 3 2 3 5 1 2 3 1 4 5 3 6
Example Output
true false
think:如果在B中能找到全部的A中的元素,A就是B的子集。
代码:
#include <bits/stdc++.h> using namespace std; int a[100005], b[100005]; int main() { int m ,n; int i, j, k; while(cin>>m>>n) { for(i = 0; i < m; i++) cin>>a[i]; for(i = 0; i < n; i++) cin>>b[i]; int flag, cnt = 1; if(m > n) cout<<"false"<<endl; else { for(j = 0; j < m; j++) { flag = 0; for(k = 0; k < n; k++) { if(b[k] == a[j]) { flag = 1; break; } } if(flag == 0) { cnt = 0; break; } } if(cnt == 0) cout<<"false"<<endl; else cout<<"true"<<endl; } } return 0; }