백준 16637 괄호추가하기 ❌

CJB_ny·2023년 2월 17일
0

백준

목록 보기
73/104
post-thumbnail

괄호 추가하기


인덱스를 기반으로 "누적합" -> 방향성 정해야함.

"완탐"을 할 때는 idx를 기반으로 뭔가를 할 생각을 해야한다.

누적합이 가능한 이유가

이런경우

이런식으로 누적합이 쌓일 것이기 때문이다.

아니면 아래와 같은 경우

그리고 숫자랑 operator랑 나누어서 계산을 해야한다.


cpp 코드

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define endl "\n"
#define ll long long
const int INF = 987654321;

vector<int> num;
vector<char> oper_str;
int n, ret = INF * -1;
string s;

int oper(char a, int b, int c)
{
    if (a == '+') return b + c;
    if (a == '-') return b - c;
    if (a == '*') return b * c;
}

void Go(int here, int _num)
{
    if (here == num.size() - 1)
    {
        ret = max(ret, _num);
        return;
    }

    Go(here + 1, oper(oper_str[here], _num, num[here + 1]));

    if (here + 2 <= num.size() - 1)
    {
        int temp = oper(oper_str[here + 1], num[here + 1], num[here + 2]);
        Go(here + 2, oper(oper_str[here], _num, temp));
    }
    return;
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin >> n;
    cin >> s;
    for (int i = 0; i < n; ++i)
    {
        if (i % 2 == 0) num.push_back(s[i] - '0');
        else oper_str.push_back(s[i]);
    }
    Go(0, num[0]);
    cout << ret << endl;

    return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글