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; }
相关文章推荐
- ZOJ 3198 Intersection of Two Sets
- zoj 3198 Intersection of Two Sets
- ZOJ 3198 Intersection of Two Sets
- ZOJ 3198 Intersection of Two Sets
- Finding intersection and union of two sets.
- 160 Intersection of Two Linked Lists
- [LintCode] Intersection of Two Arrays 两个数组相交
- LeetCode: Intersection of Two Linked Lists
- 【Leetcode】Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- LeetCode-349 Intersection of Two Arrays
- LeetCode-Intersection of Two Linked Lists
- LeetCode(160) Intersection of Two Linked Lists
- [Leetcode]Intersection of Two Arrays II
- Leetcode: Intersection of Two Linked Lists
- leetcode Intersection of Two Linked Lists 两链表是否相交
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Arrays II
- Intersection of Two Linked Lists