ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 20291 ํŒŒ์ผ ์ •๋ฆฌ with Python
    PS 2022. 1. 26. 22:51
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 20291 ํŒŒ์ผ ์ •๋ฆฌ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ๋ฐ”ํƒ•ํ™”๋ฉด์— ์žˆ๋Š” ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜ N (1 <= N <= 50000)

    2. ํŒŒ์ผ์˜ ์ด๋ฆ„์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ์ (.)์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.
      ์ ์€ ์ •ํ™•ํžˆ ํ•œ ๋ฒˆ ๋“ฑ์žฅํ•˜๋ฉฐ, ํŒŒ์ผ ์ด๋ฆ„์˜ ์ฒซ ๊ธ€์ž ๋˜๋Š” ๋งˆ์ง€๋ง‰ ๊ธ€์ž๋กœ ์˜ค์ง€ ์•Š๋Š”๋‹ค.
      ๊ฐ ํŒŒ์ผ์˜ ์ด๋ฆ„์˜ ๊ธธ์ด๋Š” ์ตœ์†Œ 3, ์ตœ๋Œ€ 100์ด๋‹ค.

    3. ํ™•์žฅ์ž์˜ ์ด๋ฆ„๊ณผ ๊ทธ ํ™•์žฅ์ž ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋ฅผ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•œ๋‹ค.
      ํ™•์žฅ์ž๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋Š” ๊ฒฝ์šฐ ํ™•์žฅ์ž ์ด๋ฆ„์˜ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

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

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

    from sys import stdin
    
    n = int(stdin.readline())
    file_name = {}
    for i in range(n):
        data = stdin.readline().rstrip().split('.')
        if data[1] in file_name:
            file_name[data[1]] += 1
        else:
            file_name[data[1]] = 1
    
    res = []
    for key, item in file_name.items():
        res.append((key, item))
    
    res.sort()
    for i in res:
        print(*i)

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

    ์˜ˆ์ œ

    8
    sbrus.txt
    spc.spc
    acm.icpc
    korea.icpc
    sample.txt
    hello.world
    sogang.spc
    example.txt

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

    icpc 2
    spc 2
    txt 3
    world 1

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

    1. ํ™•์žฅ์ž์˜ ์ด๋ฆ„๊ณผ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•  dict ์ž๋ฃŒํ˜• ๋ณ€์ˆ˜ ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ ๋‹ค.

    2. split() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด '.'์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•œ ํ›„ ํ™•์žฅ์ž ๋ช…์ด dict์— ์žˆ์„ ๊ฒฝ์šฐ + 1, ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•œ ํ›„ 1๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.

    3. dict_name.items() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, key, value ์Œ์„ res ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๊ณ  ์ •๋ ฌํ•œ ๋’ค ์ถœ๋ ฅํ•œ๋‹ค.

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

    1. ํ•ด์‹œ๋งต์„ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€์—ˆ๋˜ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.