euisblue
17682. 다트 게임

Level 1다트 게임

시간 복잡도: O(N), N = len(str)

1#include <bits/stdc++.h> 2using namespace std; 3 4int solution(string dartResult) { 5 stack<int> s; 6 string str = dartResult; 7 const int SIZE = str.size(); 8 9 for(int i=0; i<SIZE; ++i) { 10 if(isdigit(str[i]) && isdigit(str[i+1])) { 11 s.push(10); 12 ++i; 13 } 14 else if (isdigit(str[i])) { 15 s.push(str[i]-'0'); 16 } 17 else if(isalpha(str[i])) { 18 int pts = s.top(); s.pop(); 19 if(str[i]=='D') pts *= pts; 20 if(str[i]=='T') pts *= pts * pts; 21 s.push(pts); 22 } 23 else if(str[i]=='*') { 24 int curr = s.top(); s.pop(); 25 curr *= 2; 26 if(s.empty()) { 27 s.push(curr); 28 continue; 29 } 30 int prev = s.top(); s.pop(); 31 prev *= 2; 32 s.push(prev); 33 s.push(curr); 34 } 35 else if(str[i]=='#') { 36 int pts = s.top(); s.pop(); 37 s.push(pts*-1); 38 } 39 } 40 41 int sum = 0; 42 while(!s.empty()) { 43 sum += s.top(); 44 s.pop(); 45 } 46 return sum; 47}

스타상(*)때문에 이전 점수도 저장을 하고 있어야 하기 때문에 스택을 사용했다. 스택을 사용해서 모든 값을 저장하고, 마지막에 스택에 남아있는 점수들을 전부 더해주었다.

카카오톡 측의 문제해설은 여기를 참고..