Search I
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 line q is given. Then, in the fourth line, q integers are given.
Output
Print C in a line.
Constraints
n ≤ 10000
q ≤ 5000 ≤ an element in S ≤ 1090 ≤ an element in T ≤ 109
Sample Input 15123453341
Sample Output 13
Sample Input 2331215
Sample Output 20
Sample Input 3511223212
Sample Output 32
代码块
#include<iostream>usingnamespace std;intSearch(int*a,int n,int key){int i =0;
a[n]= key;while(a[i]!=key)
i++;return i!=n;}intmain(void){int i, n, q;
cin>>n;int a[n+1];for(i=0; i<n; i++)
cin>>a[i];
cin>>q;int sum =0;for(i=0; i<q; i++){int key;
cin>>key;if(Search(a, n, key))
sum++;}
cout<<sum<<endl;return0;}
题解向线性搜索中引入”标记“可以将算法效率提高常数倍。所谓标记,就是在数组等数据结构中设置的一个拥有特殊值的元素。借助这项编程技巧,可以达到简化循环控制等诸多目的。在这里的线性搜索中,将含有目标关键字的数据作为标记放在数组的第n位。这样从前往后查找,如果在第n位才查找成功就说明查找失败。题目Search IYou are given a sequence of n integers S and a sequence of different q integers T. Write a prog