Code[VS] 1230 題解

來源:互聯網
上載者:User

標籤: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 題解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.