Linear Search
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的更多相关文章
- [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. ...
- 有序线性搜索(Sorted/Ordered Linear Search)
如果数组元素已经排过序(升序),那我们搜索某个元素就不必遍历整个数组了.在下面给出的算法代码中,到任何一点,假设当前的arr[i]值大于搜索的值data,就可以停止搜索了. #include<s ...
- 无序线性搜索(Unordered Linear Search)
假定有一个元素顺序情况不明的数组.这种情况如果我们要搜索一个元素就要遍历整个数组,才能知道这个元素是否在数组中. 这种方法要检查整个数组,核对每个元素.下面是算法实现: #include<std ...
- LeetCode编程训练 - 折半查找(Binary Search)
Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...
- 【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 ...
- 算法与数据结构基础 - 折半查找(Binary Search)
Binary Search基础 应用于已排序的数据查找其中特定值,是折半查找最常的应用场景.相比线性查找(Linear Search),其时间复杂度减少到O(lgn).算法基本框架如下: //704. ...
- 【转】MySQL索引背后的数据结构及算法原理
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
- [转]MySQL索引背后的数据结构及算法原理
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
- MySQL索引背后的数据结构及算法原理【转】
本文来自:张洋的MySQL索引背后的数据结构及算法原理 摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持 ...
随机推荐
- 在JSP中将EXEL文件的数据传入到数据库中
在jsp中: 在script中使用函数: $(function(){ //var lpyear = document.getElementById("lpyear").value; ...
- String.replace使用技巧
relace replace() 方法返回一个由替换值替换一些或所有匹配的模式后的新字符串.模式可以是一个字符串或者一个正则表达式, 替换值可以是一个字符串或者一个每次匹配都要调用的函数. 使用字符串 ...
- Css3新增背景属性
1.background-origin 背景的起始位置 background-origin: border-box || padding-box || content-box; 案例初始化: 代码: ...
- 201610-H5项目总结
1.首屏进入动效使用jQuery的animate(); $('.btn_driver').animate({ left:'26%' },'slow'); $('.btn_show').animate( ...
- The method setItems(String) in the type ForTokensTag is not applicable for the arguments (Object)
1. 问题 看到这个错误以为是貌似jsp页面有误,c:forTokens标签用错了?? An error occurred at line: in the jsp file: /WEB-INF/pag ...
- 关于 ie9 不执行 js 的问题
当写js用 console.log()调试,但没有注释,在ie 9 浏览器上运行时,console.log() 后面的脚本不能再执行了. 为了ie,记得把console.log() 注释
- <Android 应用 之路> MPAndroidChart~BarChart
简介 MPAndroidChart是PhilJay大神给Android开发者带来的福利.MPAndroidChart是一个功能强大并且使用灵活的图表开源库,支持Android和IOS两种,这里我们暂时 ...
- linux 系统开机自启执行 操作的配置
1 linux 服务注册 service文件 在service文件中设置变量和环境变量 [Unit] Description= #服务描述 After=syslog.target #服务启动依赖 [S ...
- 使用Mist部署Contract到Rinkeby以太坊网络
本文使用MyEthWallet新建一个账号,并导入到Mist中,然后部署Contract到Rinkeby网络使用MyEthWallet新建账号的好处是除了JSON文件之外,还能得到一张它生成的pdf( ...
- 信用卡精养卡POS机方案
所谓的精养卡,就是模仿有钱人的一种方式,提额难吗!真心不难,难就难在养卡消费 ,信用卡都有,但是不同费率的POS机你有吗,没有POS机难道你真的要去花费去消费吗,你消费的起吗?所以我们这个行业就出现了 ...