29 lines
810 B
Swift
29 lines
810 B
Swift
func dfs(_ N: Int, _ M: Int, _ nums: [Int], _ current: Int, _ result: inout [Int]) {
|
|
if result.count == M {
|
|
for i in 0..<M {
|
|
print(result[i], terminator: " ")
|
|
}
|
|
print()
|
|
return
|
|
}
|
|
|
|
var prev = -1
|
|
for i in current..<N {
|
|
if prev != nums[i] {
|
|
result.append(nums[i])
|
|
dfs(N,M,nums,i,&result)
|
|
_ = result.popLast()
|
|
prev = nums[i]
|
|
}
|
|
}
|
|
}
|
|
|
|
if let line1 = readLine(), let NM = line1.split(separator: " ").compactMap({Int($0)}) as? [Int], NM.count == 2 {
|
|
let N = NM[0]
|
|
let M = NM[1]
|
|
|
|
if let line2 = readLine(), let nums = line2.split(separator: " ").compactMap({Int($0)}) as? [Int], nums.count == N {
|
|
var result: [Int] = [];
|
|
dfs(N,M,nums.sorted(),0,&result)
|
|
}
|
|
} |