ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 14725 ๊ฐœ๋ฏธ๊ตด with Python
    PS 2021. 11. 21. 17:49
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 14725 ๊ฐœ๋ฏธ๊ตด

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ์ฒซ ๋ฒˆ์งธ ์ค„์€ ๋กœ๋ด‡ ๊ฐœ๋ฏธ๊ฐ€ ๊ฐ ์ธต์„ ๋”ฐ๋ผ ๋‚ด๋ ค์˜ค๋ฉด์„œ ์•Œ๊ฒŒ ๋œ ๋จน์ด์˜ ์ •๋ณด ๊ฐœ์ˆ˜ N >> (1 โ‰ค N โ‰ค 1000)
      ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N+1 ๋ฒˆ์งธ ์ค„๊นŒ์ง€, ๊ฐ ์ค„์˜ ์‹œ์ž‘์€ ๋กœ๋ด‡ ๊ฐœ๋ฏธ ํ•œ๋งˆ๋ฆฌ๊ฐ€ ๋ณด๋‚ด์ค€ ๋จน์ด ์ •๋ณด ๊ฐœ์ˆ˜ K >> (1 โ‰ค K โ‰ค 15)
      ๋‹ค์Œ K๊ฐœ์˜ ์ž…๋ ฅ์€ ๋กœ๋ด‡ ๊ฐœ๋ฏธ๊ฐ€ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ ์ธต๋งˆ๋‹ค ์ง€๋‚˜์˜จ ๋ฐฉ์— ์žˆ๋Š” ๋จน์ด ์ •๋ณด์ด๋ฉฐ ๋จน์ด ์ด๋ฆ„ ๊ธธ์ด t >> (1 โ‰ค t โ‰ค 15)

    2. ํŠธ๋ผ์ด(Trie) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ.

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

    from sys import stdin
    n = int(stdin.readline())
    
    
    class Trie:
        def __init__(self):
            self.root = {}
    
        def insert(self, s):
            cur_node = self.root
            for c in s:
                if c not in cur_node:
                    cur_node[c] = {}
                cur_node = cur_node[c]
            cur_node['*'] = {}
    
        def print_trie(self, l, cur_node=None):
            if l == 0:
                cur_node = self.root
    
            for c in sorted(cur_node.keys()):
                if c != '*':
                    print('--' * l, c, sep="")
                self.print_trie(l + 1, cur_node[c])
    
    
    trie = Trie()
    for _ in range(n):
        data = list(stdin.readline().split())
        trie.insert(data[1:])
    
    trie.print_trie(0)

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

    ์˜ˆ์ œ

    4
    2 KIWI BANANA
    2 KIWI APPLE
    2 APPLE APPLE
    3 APPLE BANANA KIWI

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

    APPLE
    --APPLE
    --BANANA
    ----KIWI
    KIWI
    --APPLE
    --BANANA

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

    1. trie ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๋กœ๋ด‡ ๊ฐœ๋ฏธ๊ฐ€ ๋ณด๋‚ด์ค€ ๋ฐ์ดํ„ฐ๋ฅผ Trie ์— ๋‹ด๋Š”๋‹ค.
    1. ํด๋ž˜์Šค ๋‚ด insert ํ•จ์ˆ˜์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋„ฃ๋Š”๋‹ค.
      cur_node ๋ณ€์ˆ˜๋ฅผ root๋กœ ์ฃผ๊ณ , ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด cur_node๋ฅผ ๊ฐฑ์‹ ,
      ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
      ์ž…๋ ฅ ์ž‘์—… ๋ฐ˜๋ณต๋ฌธ์ด ๋์ด ๋‚ฌ๋‹ค๋ฉด, ๋…ธ๋“œ์˜ ๋งˆ์ง€๋ง‰์„ '*' ๋กœ ์ €์žฅํ•ด์ค€๋‹ค.
    1. ๋งˆ์ง€๋ง‰ ๋ผ์ธ์—์„œ ํด๋ž˜์Šค ๋‚ด ์ถœ๋ ฅ ํ•จ์ˆ˜์ธ, print_trie ๋ฅผ 0 ์„ ๋„ฃ์–ด ์‹คํ–‰ํ•ด์ค€๋‹ค.
      l ์ด 0์ด๋ผ๋ฉด, cur_node ๋ฅผ root๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.
      cur_node์˜ ํ‚ค ๊ฐ’์„ ์ •๋ ฌํ•œ ๋’ค, ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ์ถœ๋ ฅํ•ด์ค€๋‹ค
      ์—ฌ๊ธฐ์„œ ๋…ธ๋“œ์˜ ํ‚ค ๊ฐ’์ด '*' ์ด๋ผ๋ฉด ์ถœ๋ ฅ์„ ํ•˜์ง€ ์•Š๊ณ , print_trie ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ ํ˜ธ์ถœํ•ด์ค€๋‹ค.
      ์ด๋•Œ, l์˜ ๊ฐ’์€ + 1, cur_node๋Š” c๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋„ฃ์–ด์ค€๋‹ค.
    1. ๋งŒ์•ฝ cur_node ์˜ ํ‚ค ๊ฐ’์ด '*' ์ด ์•„๋‹ˆ๋ผ๋ฉด '--' ๋ฌธ์ž์—ด์„ l ๋งŒํผ ์ถœ๋ ฅํ•œ ๋’ค, ๋…ธ๋“œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค

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

    1. Trie ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ณต๋ถ€ํ•ด์•ผ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.
    2. ์ฒ˜์Œ๋ณด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๊ธฐ์— ์ดํ•ดํ•˜๋Š”๋ฐ์—๋„ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๊ณ , ๋ถ€์กฑํ•œ ์ ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
    3. ๋กœ์ง์„ ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•ด์•ผํ•  ๊ฒƒ์„ ๋‹ค์‹œ ํ•œ๋ฒˆ ์ •๋ฆฌํ•˜๊ณ  ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.