ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

16943๋ฒˆ: ์ˆซ์ž ์žฌ๋ฐฐ์น˜

๋‘ ์ •์ˆ˜ A์™€ B๊ฐ€ ์žˆ์„ ๋•Œ, A์— ํฌํ•จ๋œ ์ˆซ์ž์˜ ์ˆœ์„œ๋ฅผ ์„ž์–ด์„œ ์ƒˆ๋กœ์šด ์ˆ˜ C๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ์ฆ‰, C๋Š” A์˜ ์ˆœ์—ด ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋˜์–ด์•ผ ํ•œ๋‹ค.  ๊ฐ€๋Šฅํ•œ C ์ค‘์—์„œ B๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด์„œ, ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•ด๋ณด๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 A, B์˜ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ A์˜ ์ž๋ฆฟ์ˆ˜๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ์ˆœ์—ด์„ ๊ตฌํ•˜์—ฌ B๋ณด๋‹ค ์ž‘์œผ๋ฉด์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. `DFS`๋ฅผ ํ†ตํ•ด ์ˆœ์—ด์„ ๊ตฌํ•˜๋ฉด์„œ, B๋ณด๋‹ค ์ž‘์€์ง€ ํ™•์ธํ•˜๊ณ  ์ตœ๋Œ€ ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค. ์ค‘๊ฐ„์— ๊ฐ€์ง€์น˜๊ธฐ๋ฅผ ํ•ด์ฃผ๊ณ  ์‹ถ์–ด์„œ, ์ˆœ์—ด์˜ ์ฒ˜์Œ ๊ฐ’์ด B์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ฐ€์ง€๋ฅผ ๋ป—์ง€ ์•Š๊ฒŒ ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ... 74~75% ์ •๋„์—์„œ `ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.` ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ€์ง€์น˜์ง€๋ฅผ ํ•˜์ง€ ์•Š์œผ๋‹ˆ ์ •๋‹ต์„ ๋งž์ถœ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


def dfs(idx, cur_num):
    global answer

    if idx == length:
        if int(cur_num) <= int(''.join(map(str, b))):
            answer = max(answer, int(cur_num))
        return

    for i in range(length):
        if idx == 0 and a[i] == 0:
            continue

        if not check[i]:
            check[i] = True
            dfs(idx + 1, cur_num + str(a[i]))
            check[i] = False


if __name__ == '__main__':
    answer = -1
    a, b = stdin.readline().split()
    a = list(map(int, a.strip()))
    b = list(map(int, b.strip()))

    length = len(a)
    check = [False] * length
    dfs(0, '')

    print(answer)

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€