Skip to content

Commit

Permalink
Merge branch 'chaeshee0908'
Browse files Browse the repository at this point in the history
  • Loading branch information
chaeshee0908 committed Dec 11, 2023
2 parents e981e0c + 1207654 commit 562f570
Show file tree
Hide file tree
Showing 11 changed files with 1,327 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: ๐Ÿน 4์™€ 7
author: chaeshee0908
date: 2023-10-29 20:00:00 +09:00
categories: [์•Œ๊ณ ๋ฆฌ์ฆ˜, 4์™€ 7]
tags: [์•Œ๊ณ ๋ฆฌ์ฆ˜, ๋ฐฑ์ค€, ๊ณจ๋“œ5, ๊ตฌํ˜„, 5์ฃผ์ฐจ, ์ฑ„์Šนํฌ]
render_with_liquid: false
---

## 1. ๋ฌธ์ œ ๋งํฌ

[2877๋ฒˆ: 4์™€ 7](https://www.acmicpc.net/problem/2877)

<br>

## 2. ์ฝ”๋“œ

`Python3` `31120kb` `40ms`

```python
count = [2**i for i in range(31)]
K = int(input())

num = 0
for i in range(1, 31):
num += count[i]
if K <= num:
num_len = i # K๋ฒˆ์งธ ์ˆซ์ž์˜ ๊ธธ์ด
break

k_location = K - (num - count[num_len])
result = ''

div_num = 2
while num_len:
n = k_location % div_num
if n == 0 or n > (div_num // 2):
result = '7' + result
else:
result = '4' + result
num_len -= 1
div_num *= 2

print(int(result))
```

<br>

## 3. ํ•ด์„ค

๋จผ์ € K๋ฒˆ์งธ ์ˆซ์ž์˜ ๊ธธ์ด๋ฅผ ํŒŒ์•…ํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” `num_len`

๊ฐ€์žฅ ์ž‘์€ ์ž๋ฆฌ์ˆ˜ ๋ถ€ํ„ฐ ๊ฐ’์„ ๊ตฌํ•ด์ค๋‹ˆ๋‹ค. ๊ฐ ์ž๋ฆฌ์—๋Š” 4์™€ 7๋งŒ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” 2์˜ `num_len` ์ œ๊ณฑ๋ฐฐ ์ž…๋‹ˆ๋‹ค.

ํ™•์ธํ•˜๋Š” ์ž๋ฆฌ๊ฐ€ ํ•œ ์นธ ์•ž์œผ๋กœ ๊ฐˆ ๋•Œ๋งˆ๋‹ค `div_num`(๋‚˜๋ˆ„์–ด์ฃผ๋Š” ์ˆ˜)์— 2๋ฅผ ๊ณฑํ•ด์ค๋‹ˆ๋‹ค. ์ด๋•Œ div_num์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ `div_num`์˜ 2๋ถ„์˜ 1๋ฐฐ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด โ€˜4โ€™, ํฌ๋‹ค๋ฉด(ํ˜น์€ 0์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง„๋‹ค๋ฉด) โ€˜7โ€™์„ ๊ฐ’์— ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

K๋ฒˆ์งธ ์ˆซ์ž์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•ด์ฃผ๊ณ  ๋ฌธ์ž์—ด๋กœ ๋‹ค๋ค˜๋˜ ๊ฐ’์„ intํ˜•์œผ๋กœ ํ˜•๋ณ€ํ™˜ ํ•ด์ค๋‹ˆ๋‹ค.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: ๐Ÿน ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ
author: chaeshee0908
date: 2023-12-03 20:00:00 +09:00
categories: [์•Œ๊ณ ๋ฆฌ์ฆ˜, ๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ]
tags: [์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, level2, ๊ตฌํ˜„, 7์ฃผ์ฐจ, ์ฑ„์Šนํฌ]
render_with_liquid: false
---

## 1. ๋ฌธ์ œ ๋งํฌ

[๊ฑฐ๋ฆฌ๋‘๊ธฐ ํ™•์ธํ•˜๊ธฐ](https://school.programmers.co.kr/learn/courses/30/lessons/81302)

<br>

## 2. ์ฝ”๋“œ

```python
def solution(places):
answer = []
for place in places:
candidate = find_candidate(place)
answer.append(check_observe_distance(place, candidate))
return answer

def find_candidate(place):
candidate = []
for i in range(5):
for j in range(5):
if place[i][j] == 'P':
candidate.append((i, j))
return candidate

def check_observe_distance(place, candidate):
for i in range(len(candidate)-1):
now_x, now_y = candidate[i]
for j in range(i+1, len(candidate)):
if i == j:
continue
next_x, next_y = candidate[j]
manhattan = abs(now_x - next_x) + abs(now_y - next_y)
diff_x, diff_y = abs(now_x - next_x), abs(now_y - next_y)
# ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ๊ฐ€ 1์ดํ•˜์ธ ๊ฒฝ์šฐ
if manhattan <= 1:
return 0
# ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ๊ฐ€ 2์ธ ๊ฒฝ์šฐ
if manhattan == 2:
# ๊ฐ€๋กœ ๊ธธ์ด 2์ธ ๊ฒฝ์šฐ ์ค‘ ํŒŒํ‹ฐ์…˜ ์—†๋Š” ๊ฒฝ์šฐ
if diff_x == 2 and place[min(now_x, next_x)+1][now_y] != 'X':
return 0
# ์„ธ๋กœ ๊ธธ์ด 2์ธ ๊ฒฝ์šฐ ์ค‘ ํŒŒํ‹ฐ์…˜ ์—†๋Š” ๊ฒฝ์šฐ
elif diff_y == 2 and place[now_x][min(now_y, next_y)+1] != 'X':
return 0
# ๋Œ€๊ฐ์„ ์ธ ๊ฒฝ์šฐ ์ค‘ ํŒŒํ‹ฐ์…˜ ์—†๋Š” ๊ฒฝ์šฐ
elif place[now_x][next_y] == 'O' or place[next_x][now_y] == 'O':
return 0
return 1
```
- ์‹คํ–‰ ๊ฒฐ๊ณผ

| ํ…Œ์ŠคํŠธ 1 ใ€‰ | ํ†ต๊ณผ (0.16ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 2 ใ€‰ | ํ†ต๊ณผ (0.05ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 3 ใ€‰ | ํ†ต๊ณผ (0.04ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 4 ใ€‰ | ํ†ต๊ณผ (0.06ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 5 ใ€‰ | ํ†ต๊ณผ (0.04ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 6 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 7 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 8 ใ€‰ | ํ†ต๊ณผ (0.08ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 9 ใ€‰ | ํ†ต๊ณผ (0.04ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 10 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 11 ใ€‰ | ํ†ต๊ณผ (0.04ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 12 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 13 ใ€‰ | ํ†ต๊ณผ (0.04ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 14 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 15 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 16 ใ€‰ | ํ†ต๊ณผ (0.06ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 17 ใ€‰ | ํ†ต๊ณผ (0.05ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 18 ใ€‰ | ํ†ต๊ณผ (0.06ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 19 ใ€‰ | ํ†ต๊ณผ (0.07ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 20 ใ€‰ | ํ†ต๊ณผ (0.08ms, 10.1MB) |
| ํ…Œ์ŠคํŠธ 21 ใ€‰ | ํ†ต๊ณผ (0.07ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 22 ใ€‰ | ํ†ต๊ณผ (0.06ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 23 ใ€‰ | ํ†ต๊ณผ (0.02ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 24 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 25 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 26 ใ€‰ | ํ†ต๊ณผ (0.02ms, 10.1MB) |
| ํ…Œ์ŠคํŠธ 27 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 28 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.2MB) |
| ํ…Œ์ŠคํŠธ 29 ใ€‰ | ํ†ต๊ณผ (0.05ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 30 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 31 ใ€‰ | ํ†ต๊ณผ (0.05ms, 10.4MB) |

<br>

## 3. ํ•ด์„ค

๊ฐ•์˜์‹ค ๋ณ„ ์‘์‹œ์ž๋“ค์˜ ์œ„์น˜ ์ •๋ณด๋ฅผ ๋‹ด์€ candidate๋ฅผ ์ƒ์„ฑํ•œ ํ›„, ๊ฐ•์˜์‹ค ๋ณ„ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ํŒŒ์•…ํ•œ๋‹ค.

๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1. ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ = 1
2. ๋งจํ—ˆํŠผ ๊ฑฐ๋ฆฌ = 2
1. ๊ฐ€๋กœ ๊ฑฐ๋ฆฌ ํ˜น์€ ์„ธ๋กœ ๊ฑฐ๋ฆฌ๊ฐ€ 2์ฐจ์ด ์ผ๋•Œ ๊ฐ€์šด๋ฐ ์œ„์น˜์— ํŒŒํ‹ฐ์…˜์ด ์—†๋Š” ๊ฒฝ์šฐ
2. ๋Œ€๊ฐ์„ ์— ์‘์‹œ์ž๋“ค์ด ์œ„์น˜ํ•  ๋•Œ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ ๋Œ€๊ฐ์„  ์ค‘ ํŒŒํ‹ฐ์…˜์ด ํ•˜๋‚˜๋ผ๋„ ์—†๋Š” ๊ฒฝ์šฐ

candidate ์œ„์น˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ์œ„์˜ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์ผ ๋•Œ๋Š” 0์„ ๋ฆฌํ„ด, ๊ทธ ์ด์™ธ๋Š” 1์„ ๋ฆฌํ„ดํ•˜๋Š” check_observe_distance ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ ํ›„ answer์— ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: ๐Ÿน ๋ฏธ๋กœ ํƒˆ์ถœ ๋ช…๋ ์–ด
author: chaeshee0908
date: 2023-12-03 20:00:00 +09:00
categories: [์•Œ๊ณ ๋ฆฌ์ฆ˜, ๋ฏธ๋กœ ํƒˆ์ถœ ๋ช…๋ ์–ด]
tags: [์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, level3, ๊ตฌํ˜„, 7์ฃผ์ฐจ, ์ฑ„์Šนํฌ]
render_with_liquid: false
---

## 1. ๋ฌธ์ œ ๋งํฌ

[๋ฏธ๋กœ ํƒˆ์ถœ ๋ช…๋ ์–ด](https://school.programmers.co.kr/learn/courses/30/lessons/150365)

<br>

## 2. ์ฝ”๋“œ

```python
dx = [1, 0, 0, -1]
dy = [0, -1, 1, 0]
move = ['d', 'l', 'r', 'u']

from collections import deque
import sys

def solution(n, m, x, y, r, c, k):
sys.setrecursionlimit(5000)
q = deque([])
q.append((x, y, 0, ''))
# ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ธธ์ด
min_dist = abs(x-r) + abs(y-c)

# ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ์ด๋™ ๊ฐ€๋Šฅ ๊ฑฐ๋ฆฌ๊ฐ€ ์ž‘๊ฑฐ๋‚˜ ๋„๋‹ฌ์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ์œ„์น˜์ผ ๋•Œ
if min_dist > k or (k - min_dist) % 2 == 1:
return 'impossible'

def find_path(a, b, count, path):
# k๋ฒˆ์œผ๋กœ ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
if a == r and b == c and (k-count) % 2 == 1:
return 'impossible'
# k๋ฒˆ์งธ ๋„๋‹ฌ
if a == r and b == c and count == k:
return path
for i in range(4):
na = a + dx[i]
nb = b + dy[i]
if 0 < na <= n and 0 < nb <= m:
# ์ด๋™ ์œ„์น˜๊ฐ€ ์ตœ์ข… ์œ„์น˜์— ๋„๋‹ฌํ•  ์ˆ˜ ์—†์„๋งŒํผ ๋ฉ€์–ด์งˆ ๋•Œ
if abs(na-r) + abs(nb-c) > k - (count+1):
continue
result = find_path(na, nb, count+1, path + move[i])
return result

return find_path(x, y, 0, '')
```

- ์ •ํ™•์„ฑ

| ํ…Œ์ŠคํŠธ 1 ใ€‰ | ํ†ต๊ณผ (0.21ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 2 ใ€‰ | ํ†ต๊ณผ (0.15ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 3 ใ€‰ | ํ†ต๊ณผ (0.01ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 4 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 5 ใ€‰ | ํ†ต๊ณผ (0.04ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 6 ใ€‰ | ํ†ต๊ณผ (0.02ms, 10.4MB) |
| ํ…Œ์ŠคํŠธ 7 ใ€‰ | ํ†ต๊ณผ (0.02ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 8 ใ€‰ | ํ†ต๊ณผ (0.03ms, 10.3MB) |
| ํ…Œ์ŠคํŠธ 9 ใ€‰ | ํ†ต๊ณผ (10.15ms, 16MB) |
| ํ…Œ์ŠคํŠธ 10 ใ€‰ | ํ†ต๊ณผ (7.63ms, 16.1MB) |
| ํ…Œ์ŠคํŠธ 11 ใ€‰ | ํ†ต๊ณผ (9.03ms, 16.1MB) |
| ํ…Œ์ŠคํŠธ 12 ใ€‰ | ํ†ต๊ณผ (6.72ms, 16MB) |
| ํ…Œ์ŠคํŠธ 13 ใ€‰ | ํ†ต๊ณผ (7.26ms, 16.1MB) |
| ํ…Œ์ŠคํŠธ 14 ใ€‰ | ํ†ต๊ณผ (8.84ms, 16MB) |
| ํ…Œ์ŠคํŠธ 15 ใ€‰ | ํ†ต๊ณผ (6.66ms, 16.2MB) |
| ํ…Œ์ŠคํŠธ 16 ใ€‰ | ํ†ต๊ณผ (6.79ms, 15.7MB) |
| ํ…Œ์ŠคํŠธ 17 ใ€‰ | ํ†ต๊ณผ (6.54ms, 15.9MB) |
| ํ…Œ์ŠคํŠธ 18 ใ€‰ | ํ†ต๊ณผ (8.29ms, 15.9MB) |
| ํ…Œ์ŠคํŠธ 19 ใ€‰ | ํ†ต๊ณผ (6.31ms, 16.2MB) |
| ํ…Œ์ŠคํŠธ 20 ใ€‰ | ํ†ต๊ณผ (9.90ms, 15.8MB) |
| ํ…Œ์ŠคํŠธ 21 ใ€‰ | ํ†ต๊ณผ (8.67ms, 15.8MB) |
| ํ…Œ์ŠคํŠธ 22 ใ€‰ | ํ†ต๊ณผ (8.65ms, 15.8MB) |
| ํ…Œ์ŠคํŠธ 23 ใ€‰ | ํ†ต๊ณผ (13.91ms, 16.1MB) |
| ํ…Œ์ŠคํŠธ 24 ใ€‰ | ํ†ต๊ณผ (9.83ms, 16.1MB) |
| ํ…Œ์ŠคํŠธ 25 ใ€‰ | ํ†ต๊ณผ (6.76ms, 15.8MB) |
| ํ…Œ์ŠคํŠธ 26 ใ€‰ | ํ†ต๊ณผ (9.31ms, 15.8MB) |
| ํ…Œ์ŠคํŠธ 27 ใ€‰ | ํ†ต๊ณผ (9.86ms, 16MB) |
| ํ…Œ์ŠคํŠธ 28 ใ€‰ | ํ†ต๊ณผ (9.50ms, 15.8MB) |
| ํ…Œ์ŠคํŠธ 29 ใ€‰ | ํ†ต๊ณผ (9.52ms, 16MB) |
| ํ…Œ์ŠคํŠธ 30 ใ€‰ | ํ†ต๊ณผ (9.42ms, 16MB) |
| ํ…Œ์ŠคํŠธ 31 ใ€‰ | ํ†ต๊ณผ (0.01ms, 10.3MB) |

<br>

## 3. ํ•ด์„ค

์ด ๋ฌธ์ œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ์ž˜ ๊ฑธ๋Ÿฌ๋‚ด์ฃผ์–ด ์‹œ๊ฐ„์„ ์ค„์ด๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1. (x, y) โ†’ (r, c)์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ณด๋‹ค k(์ด๋™ ๊ฐ€๋Šฅ ๊ฑฐ๋ฆฌ)๊ฐ€ ์ž‘์„ ๋•Œ
2. k(์ด๋™ ๊ฐ€๋Šฅ ๊ฑฐ๋ฆฌ)์—์„œ (x, y) โ†’ (r, c)์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ธธ์ด๋ฅผ ๋บ€ ๊ธธ์ด๊ฐ€ 2์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ ๋•Œ

โ†’ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ด๋™ ํ›„ ์ขŒ,์šฐ ํ˜น์€ ์œ„,์•„๋ž˜๋กœ ์›€์ง์—ฌ ๊ฐ™์€ ์ž๋ฆฌ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ์–ด์•ผํ•œ๋‹ค.

3. ์ด๋™์ค‘์˜ ์œ„์น˜์—์„œ ์ตœ์ข… ์œ„์น˜๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋‚จ์€ ์›€์ง์ž„์œผ๋กœ ๊ฐˆ ์ˆ˜ ์—†์„ ๋•Œ

์ด๋•Œ ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— d(์•„๋ž˜), l(์™ผ์ชฝ), r(์˜ค๋ฅธ์ชฝ), u(์œ„์ชฝ) ์ˆœ์œผ๋กœ ์›€์ง์ด๋„๋ก ๋‚˜์—ดํ•˜๊ณ  ๊ฐ€์žฅ ์ฒซ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

์ฒ˜์Œ์—๋Š” bfs๋กœ ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์ง€๋งŒ, bfs๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ตœ์ข… ๋ชฉ์ ์ง€๋กœ ๋ฐ”๋กœ๊ฐ€์ง€์•Š๊ณ  ์ฃผ๋ณ€์„ ๋ชจ๋‘ ํ™•์ธํ•˜๊ณ  ์ตœ์ข… ๋ชฉ์ ์ง€๋กœ ๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.

dfs๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ตœ์ข… ๋ชฉ์ ์ง€๊นŒ์ง€ ๋ฐ”๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ๋ฅผ ์žฌ๊ตฌํ˜„ํ–ˆ๋‹ค. ์ด๋•Œ setrecursionlimit์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋œฐ ์ˆ˜ ์žˆ๋‹ค.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
title: ๐Ÿน ๋น„์Šทํ•œ ๋‹จ์–ด
author: chaeshee0908
date: 2023-10-22 20:00:00 +09:00
categories: [์•Œ๊ณ ๋ฆฌ์ฆ˜, ๋น„์Šทํ•œ ๋‹จ์–ด]
tags: [์•Œ๊ณ ๋ฆฌ์ฆ˜, ๋ฐฑ์ค€, ๊ณจ๋“œ4, ๋ฌธ์ž์—ด, 4์ฃผ์ฐจ, ์ฑ„์Šนํฌ]
render_with_liquid: false
---

## 1. ๋ฌธ์ œ ๋งํฌ

[2469๋ฒˆ: ๋น„์Šทํ•œ ๋‹จ์–ด](https://www.acmicpc.net/problem/2179)

<br>

## 2. ์ฝ”๋“œ

`Python3` `34072kb` `824ms`

```python
from collections import Counter

N = int(input())
words = []
max_prefix_len = 0 # ์ ‘๋‘์‚ฌ์˜ ์ตœ๋Œ€ ๊ธธ์ด
for _ in range(N):
word = input()
max_prefix_len = max(max_prefix_len, len(word))
words.append(word)

# M์˜ ๊ธธ์ด์˜ ์ ‘๋‘์‚ฌ ๋งŒ๋“ค๊ณ  ํ™•์ธ
def check_prefix(M):
prefix = []
for w in words:
prefix.append(w[:M])
dict_prefix = Counter(prefix)

result = []
for key in dict_prefix.keys():
if dict_prefix[key] > 1:
result.append(key)
return result

prefix = []
# ๊ฐ€์žฅ ๊ธด ์ ‘๋‘์‚ฌ ์ฐพ๊ธฐ
while max_prefix_len:
prefix = check_prefix(max_prefix_len)
if prefix:
break
max_prefix_len -= 1

idx = 0
# ์ ‘๋‘์‚ฌ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฐ€์žฅ ์ฒซ ๋‹จ์–ด ์ฐพ๊ธฐ(์ ‘๋‘์‚ฌ๋„ ์ฐพ๊ธฐ)
for i in range(len(words)):
if words[i][:max_prefix_len] in prefix:
prefix = words[i][:max_prefix_len]
idx = i + 1
print(words[i])
break

# ํ™•์ •๋œ ์ ‘๋‘์‚ฌ์— ํ•ด๋‹นํ•˜๋Š” ๋‘ ๋ฒˆ์งธ ๋‹จ์–ด ์ฐพ๊ธฐ
for i in range(idx, len(words)):
if words[i][:max_prefix_len] == prefix:
print(words[i])
break
```

<br>

## 3. ํ•ด์„ค

๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ๋Œ€ 20000๊ฐœ ์ด๋ฏ€๋กœ 2๊ฐœ์˜ ๋‹จ์–ด์”ฉ ์ง์„์ง€์–ด ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ํ™•์ธํ•˜์—ฌ ์ ‘๋‘์‚ฌ๋ฅผ ์ฐพ๋Š” 3์ค‘ ์ค‘์ฒฉ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋ฏ€๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•ด๋ดค๋‹ค.

๋ชจ๋“  ๋‹จ์–ด๋ฅผ ๊ณตํ†ต์ ์ธ ๊ธธ์ด๋กœ ์ž˜๋ผ ์ค‘๋ณต๋˜๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ๊ทธ๊ฒƒ์ด ๊ฒน์น˜๋Š” ์ ‘๋‘์‚ฌ๊ฐ€ ๋  ๊ฒƒ์ด๊ณ  ํ•ด๋‹น ์ ‘๋‘์‚ฌ์˜ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ๊ธธ๋„๋ก ๋งŒ๋“ค์–ด์ฃผ๋ฉด ๋œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ๊ธด ๋‹จ์–ด๋ฅผ ์ ‘๋‘์‚ฌ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ๋‘๊ณ  ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค ๊ธธ์ด๋ฅผ 1์”ฉ ์ค„์—ฌ๊ฐ€๋ฉฐ ๊ฐ€์žฅ ๊ธด ์ ‘๋‘์‚ฌ์˜ ๊ธธ์ด๋ฅผ ์ฐพ๋Š”๋‹ค.

check_prefix์—์„œ ์ž…๋ ฅ๋œ ์ ‘๋‘์‚ฌ์˜ ๊ธธ์ด๋งŒํผ ๋‹จ์–ด๋ฅผ ์ž๋ฅธ ํ›„ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋งŒ๋“ค์–ด ์ ‘๋‘์‚ฌ์˜ ๊ฐœ์ˆ˜๊ฐ€ 1์ด ๋„˜๊ฒŒ๋˜๋Š” ์ ‘๋‘์‚ฌ๊ฐ€ ๊ฐ€์žฅ ๊ธด ์ ‘๋‘์‚ฌ๊ฐ€ ๋œ๋‹ค. ์ด๋•Œ ์ด๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ ‘๋‘์‚ฌ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ผ ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋ฐฐ์—ด์— ๋‹ด์•„๋‘”๋‹ค.

์ ‘๋‘์‚ฌ๋ฅผ ์ฐพ๊ณ  ๋‚œ ํ›„ ์ž…๋ ฅ๋ฐ›์€ ์ˆœ์„œ๋Œ€๋กœ ๋‹จ์–ด๋ฅผ ํ™•์ธํ•˜๊ณ  ์ ‘๋‘์‚ฌ ๋ฐฐ์—ด์— ํฌํ•จ๋œ ์ ‘๋‘์‚ฌ๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด๋ฅผ ์ฐพ๊ณ  ์ถœ๋ ฅํ•œ๋‹ค. ์ด๋•Œ ์–ด๋–ค ์ ‘๋‘์‚ฌ๋ฅผ ์‚ฌ์šฉํ• ์ง€ ํ™•์ •๋œ๋‹ค.(์ ‘๋‘์‚ฌ ๋ฐฐ์—ด ์†์—์„œ ์ž…๋ ฅ ์ˆœ์„œ๊ฐ€ ๊ฐ€์žฅ ๋‹จ์–ด์— ํฌํ•จ๋œ ์ ‘๋‘์‚ฌ)

๋ฐœ๊ฒฌ๋œ ์œ„์น˜ ๋‹ค์Œ๋ถ€ํ„ฐ ์ ‘๋‘์‚ฌ๊ฐ€ ํฌํ•จ๋œ ๊ฐ€์žฅ ์•ž ์ˆœ์„œ ๋‹จ์–ด๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.
Loading

0 comments on commit 562f570

Please sign in to comment.