문제 : https://www.acmicpc.net/problem/10773
[알고리즘풀이]
0이 입력으로 들어오면, 가장 최근에 들어온 입력을 삭제해줘야 되는 문제이다.
즉, LIFO( Last In First Out ) 형식의 자료 구조인 스택을 이용하는 문제이다.
0이 아닌 입력을 Stack에 Push하고 0이 들어오면 Stack을 Pop하면 된다.
#include<iostream>
#include<stack>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
stack<int> s;
cin >> n;
for (int i = 0; i < n; i++) {
int input;
cin >> input;
if (input == 0)
s.pop();
else
s.push(input);
}
int answer = 0;
while (!s.empty()) {
answer += s.top();
s.pop();
}
cout << answer;
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 4949 - The Balance of the World (0) | 2019.09.19 |
---|---|
[BOJ] 9012 - Parenthesis (0) | 2019.09.19 |
[BOJ] 10828 - 스택 (0) | 2019.09.19 |
[BOJ] 5430 - Integer Lists (0) | 2019.09.18 |
[BOJ] 1021 - 회전하는 큐 (0) | 2019.09.17 |