线性搜索

Linear Search

You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.

Input

In the first line n is given. In the second line, n integers are given. In the third lineq is given. Then, in the fourth line, q integers are given.

Output

Print C in a line.

Constraints

  • n ≤ 10000
  • q ≤ 500
  • 0 ≤ an element in S ≤ 109
  • 0 ≤ an element in T ≤ 109

Sample Input 1

5
1 2 3 4 5
3
3 4 1

Sample Output 1

3

Sample Input 2

3
3 1 2
1
5

Sample Output 2

0

Notes

题目要求我们统计同时存在于两个集合的元素个数,我们只要先用数组记录一个集合,在输入第二个集合时每次输入都遍历查找一遍集合一就可以了。

代码如下:

 1     #include <iostream>  
 2     #include <cstdio>  
 3     using namespace std;  
 4       
 5     const int  maxn = 10000 + 5;  
 6       
 7     int a[maxn];  
 8     int sum = 0;  
 9     int n;  
10       
11     bool find(int key){  
12       for(int i = 1;i <= n; i++){  
13         if(a[i] == key)return true;  
14       }  
15       return false;  
16     }  
17       
18     int main(){  
19       scanf("%d",&n);  
20       for(int i = 1;i <= n; i++){  
21         scanf("%d",&a[i]);  
22       }  
23       int q;  
24       scanf("%d",&q);  
25       for(int i = 1;i <= q; i++){  
26         int x;  
27         scanf("%d",&x);  
28         if(find(x)){  
29           sum++;  
30         }  
31       }  
32       printf("%d\n",sum);  
33     }  

 

posted @ 2016-12-21 11:13  fengsz  阅读(220)  评论(0编辑  收藏  举报