[BaekJoon] ๋ฐฑ์ค€ 15666๋ฒˆ : N๊ณผ M (12)

Updated:

15666๋ฒˆ : N๊ณผ M (12)


[BaekJoon] ๋ฐฑ์ค€ 15657๋ฒˆ : N๊ณผ M (8) ๊ฐ™์€ ๋ฌธ์ œ์ธ๋ฐ ๋ฐฐ์—ด์„ ์ง์ ‘ ์ž…๋ ฅํ•œ๋‹ค๋Š” ๋ถ€๋ถ„์ด ๋‹ค๋ฅธ ๋ฌธ์ œ์ด๋‹ค.

์กฐ๊ฑด์—์„œ ๊ฐ™์€ ์ˆ˜๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ๊ณจ๋ผ๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต ๊ฐ’๋“ค์€ ์ „๋ถ€ ์—†์•ค ๋’ค ์กฐํ•ฉ์„ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

์กฐํ•ฉ์€ ์ˆœ์„œ๊ฐ€ ์ƒ๊ด€ ์—†๊ณ  ์กฐ๊ฑด์—์„œ ๋น„๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋ผ๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋’ค์— ์˜ค๋Š” ๊ฐ’์ด ์•ž์— ์˜ค๋Š” ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ์•ˆ๋œ๋‹ค.

์ด ์กฐ๊ฑด๋“ค์„ ๋‹ค ์ง€์ผœ์„œ ์กฐํ•ฉ์„ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

๋ฌผ๋ก  combination_with_replacement ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.


import sys


def DFS():
	if len(stack) == m:
		print(*stack)
		return

	for i in range(len(data)):
		if stack and stack[-1] > data[i]:
			continue

		stack.append(data[i])
		DFS()
		stack.pop()


n, m = map(int, sys.stdin.readline().rsplit())
data = sorted(list(set(map(int, sys.stdin.readline().rsplit()))))
stack = []
DFS()

import sys
from itertools import combinations_with_replacement


n, m = map(int, sys.stdin.readline().rsplit())
data = sorted(list(set(map(int, sys.stdin.readline().rsplit()))))

for val in combinations_with_replacement(data, m):
	print(*val)



Categories:

Updated:

Leave a comment