您的位置:首页 > 其它

ZOJ 3198 Intersection of Two Sets

2013-05-23 17:36 351 查看
求两个有序集合的公共元素数量.

用归并排序的那种合并方法很容易得到结果.

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 100010;
int s1[maxn], s2[maxn], n1, n2;

int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &n1);
for(int i = 0; i < n1; ++i){
scanf("%d", &s1[i]);
}
scanf("%d", &n2);
for(int i = 0; i < n2; ++i){
scanf("%d", &s2[i]);
}
int idx1 = 0, idx2 = 0, ans = 0;
while(idx1 < n1 && idx2 < n2){
if(s1[idx1] < s2[idx2]){
idx1++;
}else if(s1[idx1] > s2[idx2]){
idx2++;
}else{
idx1++;
idx2++;
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: