標籤:log eve event esc tab data start cat code
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 = 10000007 ;const int maxN = 1e5 + 1e3 ; typedef long long QAQ ;int cnt ;int Next[ MOD << 1 ] , Head[ MOD << 1 ] , End[ MOD << 1 ] ;int A[ maxN ] ;inline int INPUT ( ) { int x = 0 , f = 1 ; char ch = getchar ( ) ; while ( ch < ‘0‘ || ‘9‘ < ch ) { if ( ch == ‘-‘ ) f = -1 ; ch = getchar ( ) ; } while ( ‘0‘ <= ch && ch <= ‘9‘ ) { x = ( x << 1 ) + ( x << 3 ) + ch - ‘0‘ ; 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=1 ; 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 0 ; }View Code
2016-10-26 00:14:00
Code[VS] 1230 題解