-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1874번.swift
51 lines (45 loc) · 1 KB
/
1874번.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
40
41
42
43
44
45
46
47
48
49
50
51
// 출처 : 백준 스택 수열
// https://www.acmicpc.net/problem/1874
// 풀이 : hogumachu
import Foundation
let input = Int(readLine()!)!
var value = [Int]()
var stack = [Int]()
var array = [Int]()
var result = [String]()
var valueIndex: Int = 0
var noResult = ""
for i in 1...input {
array.append(i)
}
for _ in 0..<input {
value.append(Int(readLine()!)!)
}
stack.append(array.first!)
result.append("+")
array.removeFirst()
while stack.count != 0 || array.count != 0 {
if stack.last == value[valueIndex] {
stack.removeLast()
result.append("-")
valueIndex += 1
} else if stack.last == nil {
stack.append(array.first!)
result.append("+")
array.removeFirst()
} else if stack.last! < value[valueIndex] {
stack.append(array.first!)
result.append("+")
array.removeFirst()
} else {
noResult = "NO"
break
}
}
if noResult == "NO" {
print(noResult)
} else {
for i in result {
print(i)
}
}