仔细看题就知道这个题关键就是找几个数组的公共元素的最大值
这里可以用哈希表的方法做出来!
重要的事情说三遍
哈希表!
哈希表!
哈希表!
原理就是把对应的数值作为下标映射到哈希表里面去,如果每次进来一个数值就对相应的哈希表+1这个不失为一种好方法,对于传统的都可以解决 - -
然后给上代码即可了 - -
#include <iostream>
using namespace std;
int hash[101];
int main(){
int m,n;
while(cin>>m>>n &&m*n!=0){
memset(hash,0,sizeof(hash));
for(int j=0;j<m;j++){
int k,temp;
cin>>k;
for(int i=0;i<k;i++){
cin>>temp;
hash[temp]++;
}
}
/*for(int temp=0;temp<101;temp++){
cout<<hash[temp]<<" ";
}*/
int max = 0,pos=0;
for(int i=0;i<101;i++){
if(hash[i]>max){
max= hash[i];
pos = i;
}
}
if(max>=n){
cout<<pos<<endl;
}
else cout<<"0"<<endl;
}
}