-
1874 스택 수열코딩 테스트/백준 2022. 2. 10. 17:31
https://www.acmicpc.net/problem/1874
1874번: 스택 수열
1부터 n까지에 수에 대해 차례로 [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] 연산을 수행하면 수열 [4, 3, 6, 8, 7, 5, 2, 1]을 얻을 수 있다.
www.acmicpc.net
#include <stack> #include <iostream> using namespace std; const int MAX = 100001; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int* arr = new int[MAX]; string answer; stack<int> sequence; for (int i = 0; i < n; i++) cin >> arr[i]; int iter = 0; for (int j = 1; j <= n; j++) { sequence.push(j); answer += "+"; while (sequence.empty() == false && sequence.top() == arr[iter]) { sequence.pop(); answer += "-"; iter++; } } if (sequence.empty() == false) cout << "NO \n"; else for (const auto it : answer) cout << it << "\n"; delete[] arr; return 0; } // 블로그 참고, https://scarlettb.tistory.com/71
// TestCase 오류 코드 (NO 출력 미구현) #include <stack> #include <iostream> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; stack<int> sequence; for (int i = 1; ; ) { int temp; cin >> temp; while (sequence.empty() || sequence.top() != temp) { sequence.push(i++); cout << "+ \n"; } if (sequence.top() == temp) { sequence.pop(); cout << "- \n"; } if (sequence.empty()) break; } return 0; }