-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15663번.swift
39 lines (35 loc) · 1008 Bytes
/
15663번.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 출처 : 백준 N과 M (9)
// https://www.acmicpc.net/problem/15663
// 풀이 : hogumachu
// String Sorting에 대해 고민
// visited를 설정함으로 중복된 방문 제거
import Foundation
let input = readLine()!.split(separator: " ").map{Int(String($0))!}
let n = input[0], m = input[1]
let values: [Int] = readLine()!.split(separator: " ").map{Int(String($0))!}
var result: Set<String> = []
var selected: [Int] = []
var visited = Array(repeating: false, count: n)
select(m)
let newResult = result.sorted(by: {
$0.localizedStandardCompare($1) == .orderedAscending
})
for value in newResult {
print(value)
}
func select(_ counting: Int) {
if counting == 0 {
result.insert(selected.map{String($0)}.joined(separator: " "))
return
}
for i in 0..<n {
if visited[i] {
continue
}
visited[i] = true
selected.append(values[i])
select(counting - 1)
selected.removeLast()
visited[i] = false
}
}