ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 10546 ๋ฐฐ๋ถ€๋ฅธ ๋งˆ๋ผํ† ๋„ˆ with Python
    PS 2022. 2. 24. 18:13
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 10546 ๋ฐฐ๋ถ€๋ฅธ ๋งˆ๋ผํ† ๋„ˆ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ์ฐธ๊ฐ€์ž ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N โ‰ค 105)

    2. N๊ฐœ์˜ ์ค„์—๋Š” ์ฐธ๊ฐ€์ž์˜ ์ด๋ฆ„์ด ์ฃผ์–ด์ง„๋‹ค.

    3. N-1๊ฐœ์˜ ์ค„์—๋Š” ์™„์ฃผํ•œ ์ฐธ๊ฐ€์ž์˜ ์ด๋ฆ„์ด ์“ฐ์—ฌ์ ธ ์žˆ๋‹ค.

    4. ์ฐธ๊ฐ€์ž๋“ค์˜ ์ด๋ฆ„์€ ๊ธธ์ด๊ฐ€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 20๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๋ฌธ์ž์—ด์ด๊ณ , ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
      ์ฐธ๊ฐ€์ž๋“ค ์ค‘์—” ๋™๋ช…์ด์ธ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค.

    5. ๋ฐฑ์ค€ ๋งˆ๋ผํ†ค ๋Œ€ํšŒ์— ์ฐธ๊ฐ€ํ•ด ๋†“๊ณ  ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ๋ฐฐ๋ถ€๋ฅธ ์ฐธ๊ฐ€์ž ํ•œ ๋ช…์€ ๋ˆ„๊ตด๊นŒ?

    6. ํ•ด์‹œ๋งต ์ž๋ฃŒ๊ตฌ์กฐ ์‘์šฉ ์œ ํ˜•์˜ ๋ฌธ์ œ

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

    from sys import stdin
    
    n = int(stdin.readline())
    mara_tang = {}
    
    for _ in range(n):
        name = stdin.readline().rstrip()
        if name not in mara_tang:
            mara_tang[name] = 1
        else:
            mara_tang[name] += 1
    
    for _ in range(n - 1):
        mara_tang[stdin.readline().rstrip()] -= 1
    
    for key, item in mara_tang.items():
        if item != 0:
            print(key)
            break

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

    ์˜ˆ์ œ

    4
    mislav
    stanko
    mislav
    ana
    stanko
    ana
    mislav

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

    mislav

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

    1. ๋™๋ช…์ด์ธ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. {key: value} ํ˜•์‹์œผ๋กœ ์ €์žฅ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” Dictionary ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

    2. n ์„ ์ž…๋ ฅ๋ฐ›๊ณ , n๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๋Œ€ํšŒ์— ์ฐธ๊ฐ€ํ•œ ์ธ์›๋“ค์„ dictionary ์ž๋ฃŒ๊ตฌ์กฐ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
      {์ฐธ๊ฐ€์ž ์ด๋ฆ„ : ์ธ์› ์ˆ˜}, ์ธ์› ์ˆ˜๋Š” dictionary ์— ์ด๋ฆ„์ด ์—†์„ ๋• 1๋กœ ์ดˆ๊ธฐํ™”ํ•ด์„œ ๋„ฃ์Šต๋‹ˆ๋‹ค.

    3. n-1 ๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์™„์ฃผํ•œ ์‚ฌ๋žŒ๋“ค์˜ ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ dictionary์—์„œ -1 ํ•ด์ค๋‹ˆ๋‹ค.

    4. dictionary.item() ์„ ํ†ตํ•ด์„œ key, value ๊ฐ’์„ ์ˆœํšŒํ•˜๋ฉด์„œ value ๊ฐ€ 0๋ณด๋‹ค ํฐ ํ‚ค ๊ฐ’์„ ์ฐพ์Šต๋‹ˆ๋‹ค.

    5. ์ฐธ๊ฐ€์ž ํ•œ๋ช…์„ ์ฐพ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅํ•œ ๋’ค ๋ฐ”๋กœ break ๋ฅผ ๊ฑธ์–ด ๋ฉˆ์ถฐ์ค๋‹ˆ๋‹ค.

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

    1. ํŒŒ์ด์ฌ ๋”•์…”๋„ˆ๋ฆฌ ๋งŒ์„ธ
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.