-
스택_백준_키로거_5397Algorithm 2018. 8. 15. 14:20
이 문제는 백준 #1406 문제와 비슷하게 풀 수 있다. 커서를 기준으로 왼/오른쪽 스택을 두어 처리 할 수있다. 이 문제 역시 시간복잡도는 O(N)이다.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556#include <iostream>#include <string>#include <stack>using namespace std;int main() {int count;string input;stack <char> leftStack;stack <char> rightStack;int length = 0;cin >> count;for (int k = 0; k < count; k++) {cin >> input;length = input.size();for (int i = 0; i < length; i++) {if (input[i] == '<') {if (leftStack.empty()) {continue;} // exception handlingrightStack.push(leftStack.top());leftStack.pop();}else if (input[i] == '>') {if (rightStack.empty()) {continue;} // exception handlingleftStack.push(rightStack.top());rightStack.pop();}else if (input[i] == '-') {if (leftStack.empty()) {continue;} // exception handlingleftStack.pop();}else {leftStack.push(input[i]);}}//calculatewhile (!leftStack.empty()) {rightStack.push(leftStack.top());leftStack.pop();}while (!rightStack.empty()) {cout << rightStack.top();rightStack.pop();}cout << endl;//print}return 0;}cs 'Algorithm' 카테고리의 다른 글
큐_백준_이론 (0) 2018.08.15 스택_백준_문자열폭발_9935 (0) 2018.08.15 스택_백준_탑_2493 (0) 2018.08.15 스택_백준_괄호의 값_2504 (0) 2018.08.15 스택_백준_스택 수열_1874 (0) 2018.08.14