๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿฆ„ | ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต/BOJ

[Python] ๋ฐฑ์ค€ 1920 ์ˆ˜ ์ฐพ๊ธฐ

by KASSID 2022. 3. 28.

๋ชฉ์ฐจ

    728x90
    N = int(input())
    Nlist = list(map(int, input().split(' ')))
    M = int(input())
    Mlist = list(map(int, input().split(' ')))
    for i in Mlist:
        if(i in Nlist):
            print(1)
        else:
            print(0)

    ๋ฌธ์ œ

    N๊ฐœ์˜ ์ •์ˆ˜ A[1], A[2], …, A[N]์ด ์ฃผ์–ด์ ธ ์žˆ์„ ๋•Œ, ์ด ์•ˆ์— X๋ผ๋Š” ์ •์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

    ์ž…๋ ฅ

    ์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” N๊ฐœ์˜ ์ •์ˆ˜ A[1], A[2], …, A[N]์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” M๊ฐœ์˜ ์ˆ˜๋“ค์ด ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ด ์ˆ˜๋“ค์ด A์•ˆ์— ์กด์žฌํ•˜๋Š”์ง€ ์•Œ์•„๋‚ด๋ฉด ๋œ๋‹ค. ๋ชจ๋“  ์ •์ˆ˜์˜ ๋ฒ”์œ„๋Š” -231 ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  231๋ณด๋‹ค ์ž‘๋‹ค.

    ์ถœ๋ ฅ

    M๊ฐœ์˜ ์ค„์— ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค. ์กด์žฌํ•˜๋ฉด 1์„, ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ

    5
    4 1 5 2 3
    5
    1 3 7 9 5
    

    ์˜ˆ์ œ ์ถœ๋ ฅ

    1
    1
    0
    0
    1
    
     

    ์ฒซ ๋ฒˆ์งธ ์ฝ”๋“œ

    N = int(input())
    Nlist = list(map(int, input().split(' ')))
    M = int(input())
    Mlist = list(map(int, input().split(' ')))
    for i in Mlist:
        if(i in Nlist):
            print(1)
        else:
            print(0)

     

    ๊ฒฐ๊ณผ

    ์‹œ๊ฐ„์ดˆ๊ณผ

     

    ๋‘ ๋ฒˆ์งธ ์ฝ”๋“œ

    N = int(input())
    Nlist = set(map(int, input().split(' ')))
    M = int(input())
    Mlist = list(map(int, input().split(' ')))
    for i in Mlist:
        if(i in Nlist):
            print(1)
        else:
            print(0)

     

    ๊ฒฐ๊ณผ

    ์„ฑ๊ณต

     

    ์ฝ”๋ฉ˜ํŠธ

    ํŒŒ์ด์ฌ์˜ ์ž๋ฃŒํ˜•๊ณผ ๊ทธ์™€ ๊ด€๋ จ๋œ ๋ฉ”์†Œ๋“œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„์— ๋Œ€ํ•ด์„œ

    ์กฐ์‚ฌํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋˜ ์•„์ฃผ ๊ท€์ค‘ํ•œ ๋ฌธ์ œ์˜€๋‹ค!

     

    ๋ฆฌ์ŠคํŠธ์˜ in ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•œ ํฌํ•จ ์—ฌ๋ถ€์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ด๊ณ ,

    ์ง‘ํ•ฉ์˜ in ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•œ ํฌํ•จ ์—ฌ๋ถ€ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(1)dlek.

     

    ์ฆ‰, ํƒ์ƒ‰๋งŒ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ง‘ํ•ฉ ์ž๋ฃŒํ˜•์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์‹œ๊ฐ„๋ณต์žก๋„ ์ธก๋ฉด์—์„œ

    ํ›จ์”ฌ ๋” ํšจ์œจ์ ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ธ ๊ฒƒ์ด๋‹ค!

     

    ํŒŒ์ด์ฌ์˜ ์ž๋ฃŒํ˜•๊ณผ ๊ด€๋ จ๋œ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ํฌ์ŠคํŒ…ํ•  ๊ณ„ํš์ด๋‹ค.

    ๋Œ“๊ธ€