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 ≤ 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

Sample Input 3

5
1 1 2 2 3
2
1 2

Sample Output 3

2

向线性搜索中引入"标记"可以将算法效率提高常数倍, 所谓标记, 就是我们在数组等数据结构中设置一个拥有特殊值地元素, 从而达到简化循环控制等诸多目的.
在线性搜索中, 我们可以把含有目标关键字的数据放在数组末尾, 用作标记
#include <iostream>
using namespace std;
int a[10005], b[505];
int n, q; int search(int *a, int c)
{
int idx = 0;
a[n] = c;
while(a[idx] != a[n]) idx ++; return idx != n;
} int main()
{
int sum = 0;
cin >> n;
for(int i = 0; i < n; ++ i)
cin >> a[i]; cin >> q;
for(int i = 0; i < q; ++ i)
{
cin >> b[i];
if(search(a, b[i])) sum ++;
} cout << sum << endl; return 0;
}

  

Linear Search的更多相关文章

  1. [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript

    Binary search is an algorithm that accepts a sorted list and returns a search element from the list. ...

  2. 有序线性搜索(Sorted/Ordered Linear Search)

    如果数组元素已经排过序(升序),那我们搜索某个元素就不必遍历整个数组了.在下面给出的算法代码中,到任何一点,假设当前的arr[i]值大于搜索的值data,就可以停止搜索了. #include<s ...

  3. 无序线性搜索(Unordered Linear Search)

    假定有一个元素顺序情况不明的数组.这种情况如果我们要搜索一个元素就要遍历整个数组,才能知道这个元素是否在数组中. 这种方法要检查整个数组,核对每个元素.下面是算法实现: #include<std ...

  4. LeetCode编程训练 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

  5. 【LeetCode】35. Search Insert Position (2 solutions)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  6. 算法与数据结构基础 - 折半查找(Binary Search)

    Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...

  7. 【转】MySQL索引背后的数据结构及算法原理

    摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...

  8. [转]MySQL索引背后的数据结构及算法原理

    摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...

  9. MySQL索引背后的数据结构及算法原理【转】

    本文来自:张洋的MySQL索引背后的数据结构及算法原理 摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持 ...

随机推荐

  1. Rabbit的直连交换机direct

    直连交换机类型为:direct.加入了路由键routingKey的概念. 就是说 生产者投递消息给指定交换机的指定路由键. 只有绑定了此交换机指定路由键的消息队列才可以收到消息. 生产者: packa ...

  2. Java--详解WebService技术

    Java--详解WebService技术 一.什么是 webservice WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用jav ...

  3. sharepint 2013 添加subsite

    在用服务器端对象模型往里面添加subsite的时候,照着书上的代码,结果,失败.报错 not suported language. bing了半天,说是语言未支持,又是修改系统区域,显示语言等,还是失 ...

  4. JavaScript AMD 与CMD的代码区别

    1:CMD 依赖就近 define(function(require,export) { var b =1; var a = require("../a"); a.dosometh ...

  5. 关联函数 map 的基本用法

    1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...

  6. QML Delegate中访问该持有者的方式 附加属性(转载)

    http://blog.csdn.net/yuxiaohen/article/details/17226971   用法很奇葩记录一下,实测可以,用于弱化delegate与持有者的依赖 delegat ...

  7. jdk1.8 对数组及arrays类对数组的操作与增强

    数组的初始化有两种方式 静态初始化: 初始化时由程序员显示置顶每个数组的初始值,由系统决定数组长度.如: int[] a1 = new int[] {1,2,3,4}; 动态初始化:初始化时由程序员只 ...

  8. Java—IO流 字符流

    java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码). 文件是byte byte byte ... 的数据序列. 文本文件是文本(char)序列按照某种编码方案(uf ...

  9. update_dctcp_alpha

    /* + * Update dctcp alpha based on the ecn bit in the received packet. + * This procedure is called ...

  10. Microsoft.Exchange 发邮件

    //Microsoft.Exchange.WebServices.dll ExchangeService service = new ExchangeService(); // 获取身份验证, 能够尝 ...