ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 14425 ๋ฌธ์ž์—ด ์ง‘ํ•ฉ with Python
    PS 2022. 2. 27. 21:25
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 14425 ๋ฌธ์ž์—ด ์ง‘ํ•ฉ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. N๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ์ง‘ํ•ฉ S

    2. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” M๊ฐœ์˜ ๋ฌธ์ž์—ด ์ค‘์—์„œ ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด ์ด ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

    3. N๊ณผ M (1 โ‰ค N โ‰ค 10,000, 1 โ‰ค M โ‰ค 10,000)

    4. ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 500์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

    5. ์ง‘ํ•ฉ S์— ๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

    6. ๋ฌธ์ž์—ด, ์ž๋ฃŒ๊ตฌ์กฐ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    
    n, m = map(int, stdin.readline().split())
    strings = {}
    
    for _ in range(n):
        strings[stdin.readline().rstrip()] = 0
    
    for i in range(m):
        s = stdin.readline().rstrip()
        if s in strings:
            strings[s] += 1
    
    print(sum(strings.values()))

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    5 11
    baekjoononlinejudge
    startlink
    codeplus
    sundaycoding
    codingsh
    baekjoon
    codeplus
    codeminus
    startlink
    starlink
    sundaycoding
    codingsh
    codinghs
    sondaycoding
    startrink
    icerink

    ์‹คํ–‰๊ฒฐ๊ณผ

    4

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. n๊ฐœ์˜ ๋ฌธ์ž์—ด์„ dictionary ์ž๋ฃŒํ˜• ๋ณ€์ˆ˜์— ๋„ฃ๋Š”๋‹ค

    2. m ๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ ๋ฐ›์œผ๋ฉด์„œ ๋งŒ์•ฝ ์ž…๋ ฅ ๋ฐ›์€ ๋ฌธ์ž์—ด์ด ๋งŒ๋“ค์–ด๋‘” dictionary ๋ณ€์ˆ˜์— ์žˆ๋‹ค๋ฉด
      ๊ทธ ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” value ๊ฐ’์„ + 1 ํ•ด์ค€๋‹ค.

    3. dictionary ์˜ values ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•ด์ค€๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. ํ•ด์‹œ๋งต, dict ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฌธ์ œ์ธ๋ฐ, ๋ฌธ์ œ ํƒœ๊ทธ์— ํŠธ๋ผ์ด๋ผ๊ณ  ๋˜์–ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
    2. ํŠธ๋ผ์ด๊นŒ์ง€ ํ•„์š”ํ•œ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜, N ๊ณผ M ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๊ฐ€ 10000 ์ด๋ผ์„œ
      dict ์•ˆ์— ์ž…๋ ฅ ๋ฐ›์€ m๊ฐœ์˜ ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.