๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/Problem Solving

[์†Œํ”„ํ‹ฐ์–ด softeer] [ํ•œ์–‘๋Œ€ HCPC 2023] X marks the Spot (Lv.2) - ํŒŒ์ด์ฌ(Python)

by Jay Din 2024. 6. 25.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://softeer.ai/practice/7703

 

Softeer - ํ˜„๋Œ€์ž๋™์ฐจ๊ทธ๋ฃน SW์ธ์žฌํ™•๋ณดํ”Œ๋žซํผ

 

softeer.ai

 

์˜ค๋‹ต ํ’€์ด

์ฒ˜์Œ์— ๋ฌธ์ž์—ด์„ ๋ˆ„์ ํ•˜์—ฌ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋”๋‹ˆ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค 5๋ฒˆ๊ณผ 8๋ฒˆ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ์Šต๋‹ˆ๋‹ค.

# ์˜ค๋‹ต์ฝ”๋“œ
import sys

input = sys.stdin.readline

n = int(input())
answer = ''  # ๋ฌธ์ž์—ด ๋ˆ„์ 
for i in range(n):
    s, t = input().split()
    s = s.upper()
    t = t.upper()

    answer += t[s.find('X')]  # ๋ฌธ์ž์—ด์„ ๋งค๋ฒˆ ์ƒˆ๋กœ ์ƒ์„ฑ

print(answer)

 

์œ„ ์˜ค๋‹ต ์ฝ”๋“œ๋Š” ๋ฌธ์ž์—ด 'answer'์— ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

ํŒŒ์ด์ฌ์˜ ๋ฌธ์ž์—ด์€ ๋ถˆ๋ณ€(immutable)์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ๋•Œ๋งˆ๋‹ค ๊ธฐ์กด ๋ฌธ์ž์—ด์˜ ๋‚ด์šฉ์„ ๋ณต์‚ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ณผ์ •์ด ๋ฐ˜๋ณต๋˜๋ฉด, ํŠนํžˆ ๋ฐ˜๋ณต ํšŸ์ˆ˜๊ฐ€ ๋งŽ์„ ๋•Œ ๋งค์šฐ ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋˜ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์‹œ๊ฐ„ ๋ณต์žก๋„

  • ์ฒซ ๋ฒˆ์งธ ๋ฐ˜๋ณต: O(1) (answer๊ฐ€ ์งง์Œ)
  • ๋‘ ๋ฒˆ์งธ ๋ฐ˜๋ณต: O(2)
  • ์„ธ ๋ฒˆ์งธ ๋ฐ˜๋ณต: O(3)
  • ...
  • n๋ฒˆ์งธ ๋ฐ˜๋ณต: O(n)

 

 

์ •๋‹ต ํ’€์ด

import sys

input = sys.stdin.readline

n = int(input())
answer = []  # ๋ฆฌ์ŠคํŠธ์— ๋ฌธ์ž์—ด ๋ˆ„์ 
for i in range(n):
    s, t = input().split()
    s = s.upper()
    t = t.upper()

    answer.append(t[s.find('X')])  # ๋ฆฌ์ŠคํŠธ์— ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€

print(''.join(answer))  # ์ตœ์ข…์ ์œผ๋กœ ํ•œ ๋ฒˆ์— ๋ฌธ์ž์—ด ์ƒ์„ฑ

 

์œ„ ์ •๋‹ต ์ฝ”๋“œ๋Š” ๋ฆฌ์ŠคํŠธ 'answer'์— ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ€๋ณ€(mutable) ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ƒˆ๋กœ์šด ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•  ๋•Œ ๊ธฐ์กด ๋ฆฌ์ŠคํŠธ์˜ ๋‚ด์šฉ์„ ๋ณต์‚ฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ฆฌ์ŠคํŠธ์— ๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ์ถ”๊ฐ€ํ•œ ํ›„, join ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ข… ๋ฌธ์ž์—ด์„ ํ•œ ๋ฒˆ์— ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

์‹œ๊ฐ„ ๋ณต์žก๋„

for ๋ฃจํ”„์˜ ์ „์ฒด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ด๋ฉฐ, ๋งˆ์ง€๋ง‰์— ''.join(answer)๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์น˜๋Š” ์—ฐ์‚ฐ์œผ๋กœ O(n) ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค.

  • (๋ฐ˜๋ณต) + O(n) (join ์—ฐ์‚ฐ)

 

์ •๋ฆฌํ•˜๋ฉด

  • ์˜ค๋‹ต ์ฝ”๋“œ์—์„œ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ด ๋ฐ˜๋ณต์ ์œผ๋กœ ์ผ์–ด๋‚˜๋ฉด์„œ ๊ฐ ๋ฐ˜๋ณต๋งˆ๋‹ค answer๊ฐ€ ์ ์  ๊ธธ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(N^2)์œผ๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • ์ •๋‹ต ์ฝ”๋“œ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ์— ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์—ฐ์‚ฐ์ด ํ‰๊ท  O(1) ์‹œ๊ฐ„์œผ๋กœ ํšจ์œจ์ ์ด๋ฉฐ, ๋งˆ์ง€๋ง‰์— ํ•œ ๋ฒˆ์˜ join ์—ฐ์‚ฐ์œผ๋กœ ์ „์ฒด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ ์ •๋‹ต ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ํšจ์œจ์ ์ด๊ณ , ๋Œ€๊ทœ๋ชจ ์ž…๋ ฅ์—์„œ๋„ ์‹œ๊ฐ„ ์ดˆ๊ณผ ์—†์ด ๋™์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•