용해 과정
1. 입력 시퀀스를 배열에 저장하고 스택을 만듭니다.
2. 오름차순으로 푸시해야 하므로 for 문을 사용하여 1부터 시작하여 스택에 푸시하고 “+”를 내보냅니다.
입력 시퀀스의 i-1과 같으면 제거되고 “-“가 출력된다.
3. 스택에 숫자가 남아 있으면 입력 시퀀스를 생성할 수 없으므로 NO가 반환됩니다.
3-1) 예제 #2를 예로 들면, 1, 2, 5가 팝되면 순은이 오름차순으로 스택에 쌓입니다.
너무 깨끗해. 그러나 시퀀스에서 3과 4를 순서대로 선택하고 싶지만 스택의 특성상 위에서부터 배치하므로 4와 3을 빼내므로 3 !
= 4이므로 while 문에서 나오고 for -Instruction이 끝나서 열 수 없습니다.
암호
package org.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class backJoon1874 {
public static void main(String() args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder st = new StringBuilder();
int n = Integer.parseInt(br.readLine());
Stack<Integer> input = new Stack<>();
int() num = new int(n);
for(int i= 0;i <n; i++) num(i)= Integer.parseInt(br.readLine());
int chkAscend = 0;
for(int i=1;i <=n; i++){
input.add(i);
st.append("+");
st.append("\n");
while(!
input.isEmpty()){
if(num(chkAscend) !
= input.peek()){
break;
}
else{
input.pop();
st.append("-");
st.append("\n");
chkAscend++;
}
}
}
if(input.isEmpty()){
System.out.print(st);
}
else System.out.println("NO");
}
}