1230 元素查找
题目描述 Description

  给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过。

输入描述 Input Description

  第一行两个整数 n 和m。

  第二行n个正整数(1<=n<= 100000)

  第三行m个整数(1<=m<=100000)

输出描述 Output Description

  一共m行,若出现则输出YES,否则输出NO

样例输入 Sample Input

  4 2

  2 1 3 4

  1 9

样例输出 Sample Output

  YES

  NO

数据范围及提示 Data Size & Hint

  所有数据都不超过10^8

———————————————分割线———————————————

Hash Table 练习题

#include "bits/stdc++.h"

using namespace std ;
const int MOD = ;
const int maxN = 1e5 + 1e3 ;
typedef long long QAQ ; int cnt ;
int Next[ MOD << ] , Head[ MOD << ] , End[ MOD << ] ;
int A[ maxN ] ; inline int INPUT ( ) {
int x = , f = ; char ch = getchar ( ) ;
while ( ch < '' || '' < ch ) { if ( ch == '-' ) f = - ; ch = getchar ( ) ; }
while ( '' <= ch && ch <= '' ) { x = ( x << ) + ( x << ) + ch - '' ; ch = getchar ( ) ; }
return x * f ;
} inline int Hash_Function ( int x ) {return ( ( x % MOD ) + MOD ) % MOD ; } inline bool Find ( const int tmp ) {
int pos = Hash_Function ( tmp ) , y ;
for ( int i=Head[ pos ] ; y = End[ i ] , i ; i = Next[ i ] ) if ( y == tmp ) return true ;
return false ;
} inline void Hash_Link ( const int tmp , const int pos ) {
Next[ ++cnt ] = Head[ pos ] ;
Head[ pos ] = cnt ;
End[ cnt ] = tmp ;
} inline void Hash_Add ( const int tmp ) {
int pos = Hash_Function ( tmp ) , y ;
for ( int i=Head[ pos ] ; y = End[ i ] , i ; i = Next[ i ] ) if ( y == tmp ) return ;
Hash_Link ( tmp , pos ) ;
} int main ( ) {
int N = INPUT ( ) , Q = INPUT ( ) ;
for ( int i= ; i<=N ; ++i ) {
A[ i ] = INPUT ( ) ;
if ( !Find ( A[ i ] ) )
Hash_Add ( A[ i ] ) ;
}
while ( Q -- ) {
if ( Find ( INPUT ( ) ) ) cout << "YES" << endl ;
else cout << "NO" << endl ;
}
return ;
}

2016-10-26 00:14:00

Code[VS] 1230 题解的更多相关文章

  1. Code[VS] 1332 题解 【Kosaraju】【Tarjan】

    Code[VS] 1332 上白泽慧音题解 Tarjan Algorithm Kosaraju Algorithm 题目传送门:http://codevs.cn/problem/1332/   题目描 ...

  2. 洛谷 P4058 [Code+#1]木材 题解

    P4058 [Code+#1]木材 题目描述 有 \(n\) 棵树,初始时每棵树的高度为 \(H_i\),第 \(i\) 棵树每月都会长高 \(A_i\)​.现在有个木料长度总量为 $ S$ 的订单, ...

  3. CODE[VS] 1230 元素查找

    1.题目戳这里 2.代码: #include<iostream> #include<algorithm> using namespace std; int n,m,a[1000 ...

  4. codevs 1230 元素查找

    题目链接:http://codevs.cn/problem/1230/ 题解: 会有很多方法写这道题,写个裸的哈希练练手 #include<cstdio> ,MOD=; int n,m,h ...

  5. ACM/ICPC 之 最短路径-dijkstra范例(ZOJ2750-POJ1135(ZOJ1298))

    最短路经典算法-dijkstra范例(两道),第一道是裸的dijkstra,第二道需要枚举所有边已找到可能的情况. ZOJ2750-Idiomatic Phrases Game 题意:见Code 题解 ...

  6. ACM/ICPC 之 Prim范例(ZOJ1586-POJ1789(ZOJ2158))

    两道Prim解法范例题型,简单的裸Prim,且两题相较以边为重心的Kruskal解法而言更适合以点为重心扩展的Prim解法. ZOJ1586-QS Network 题意:见Code 题解:直接的MST ...

  7. Code[VS] 2370 LCA 题解

    Code[VS] 2370 小机房的树 题解 RMQ 树链剖分 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同 ...

  8. Code[VS] 1022 覆盖 题解

    Code[VS] 1022 覆盖 题解  Hungary Algorithm 题目传送门:Code[VS] 1022 题目描述 Description 有一个N×M的单位方格中,其中有些方格是水塘,其 ...

  9. Code[VS] 2152 滑雪题解

    Code[VS] 2152 滑雪题解 题目描述 Description trs喜欢滑雪.他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形.为了得到更快的速度,滑行 ...

随机推荐

  1. JS利用取余实现toggle多函数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout

    $interval window.setInterval的Angular包装形式.Fn是每次延迟时间后被执行的函数. 间隔函数的返回值是一个承诺.这个承诺将在每个间隔刻度被通知,并且到达规定迭代次数后 ...

  3. PHP获取某月天数

    方式一: <?php function days($year,$month){ if($month<10){ $month = '0'.$month; } if($month == 12) ...

  4. [MySQL] 分页优化

    在传统的分页思路影响下,很多人都形成了对于分页的固定理解,也就是给出select语句,先用count()函数计算出总的条目,除与每个页面大小pagesize,然后用ceil取整,得出总的页数,用lim ...

  5. sql server 2008 R2配置管理

    安装vs2013后,sql server 2008R2配置管理提示“远程过程调用失败” 这是因为vs2013自带的Microsoft SQL Server 2012Local DB与之冲突. 通过升级 ...

  6. js_闭包

    先从闭包特点解释,应该更好理解.闭包的两个特点:1.作为一个函数变量的一个引用 - 当函数返回时,其处于激活状态.2.一个闭包就是当一个函数返回时,一个没有释放资源的栈区.其实上面两点可以合成一点,就 ...

  7. android 4.4/5.1上使用aar的问题

    在Android6.0上可以使用LOCAL_STATIC_JAVA_AAR_LIBRARIES.build脚本会把指定的aar解压缩到临时目录,并将class.jar和res添加到对应的变量. LOC ...

  8. js随机生成颜色代码

    function generyRandomColor() { return '#' + ('00000' + (Math.random() * 0x1000000 << 0).toStri ...

  9. 在js中获取在css中设置的background-image值

    1. html部分 <div class="bg-color-two" id="bg_color_two" onclick="setBg(thi ...

  10. Shell 获取指定行的内容

    需求: 有一个文件,根据指定的字符串,得到该字符串上两行的内容. 文件内容如下: linux-56:# cat sys.ttconnect.ini # Copyright (C) 1999, 2006 ...