본문 바로가기

백준

2257 화학식량

흐 어렵다

스택의 한 칸이 괄호의 깊이라 생각하고 풀었다. 

 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <bitset>
#include <stdio.h>
#include <math.h>
#include <typeinfo>

#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()

using namespace std;
using i64 = long long int;
using ii = pair<int, int>;
using iis = pair<int, string>;
using ii64 = pair<i64, i64>;
using iii = tuple<int, int, int>;

int getWeight(char c) {
    if (c == 'H')
        return 1;
    if (c == 'C')
        return 12;
    if (c == 'O')
        return 16;
    return 0;
}

int main() {
    string s;
    cin >> s;
    
    stack<int> st;
    st.push(0);
    int tmp = 0;
    for (int i = 0; i < s.size(); i++) {
        
        if (s[i] == 'C' || s[i] == 'H' || s[i] == 'O') {
            tmp = getWeight(s[i]);
            
            int tp = st.top();
            st.pop();
            st.push(tp + tmp);
        }
        else if (s[i] == '(')
            st.push(0);
        else if (s[i] == ')') {
            tmp = st.top();
            st.pop();
            
            int tp = st.top();
            st.pop();
            st.push(tp + tmp);
        }
        else{
            int tp = st.top();
            st.pop();
            st.push(tp + tmp * (s[i] - '1'));
        }
    }
    
    cout << st.top();
    
    return 0;
}

'백준' 카테고리의 다른 글

11663 선분 위의 점  (0) 2021.09.24
2579 계단오르기  (0) 2021.09.24
1448 삼각형 만들기  (0) 2021.09.19
2103 직교다각형 복원  (0) 2021.09.19
16114 화살표 연산자  (0) 2021.09.14