https://www.acmicpc.net/problem/5964
5964번: Best Parenthesis
Recently, the cows have been competing with strings of balanced parentheses and comparing them with each other to see who has the best one. Such strings are scored as follows (all strings are balanced): the string "()" has score 1; if "A" has score s(A) th
www.acmicpc.net
이전의 2504 문제와 크게 다르지않은 문제다.
여기선 올바른 문자인지 확인하지않아도 되며, 계산만 하면 된다.
#include <iostream>
#define fio cin.tie(0)->sync_with_stdio(0)
using namespace std;
#include <stack>
#include <string>
long long Q = 12345678910;
int main(){
fio;
int N; cin >> N;
stack<string> stack_;
for(int i=0; i < N; i++){
long long tmp; cin >> tmp;
if(tmp == 0){// (
stack_.push("(");
}else{// )
if(stack_.top() == "("){
stack_.pop();
stack_.push("1");
}else{
long long sum = 0;
while(stack_.top() != "("){
sum += stol(stack_.top());
sum %= Q;
stack_.pop();
}
stack_.pop();
sum *=2;
sum %=Q;
stack_.push(to_string(sum));
}
}
}
long long ans = 0;
while(!stack_.empty()){
ans += stol(stack_.top());
ans %= Q;
stack_.pop();
}
cout << ans;
return 0;
}
https://www.acmicpc.net/problem/2504
2504번: 괄호의 값
4개의 기호 ‘(’, ‘)’, ‘[’, ‘]’를 이용해서 만들어지는 괄호열 중에서 올바른 괄호열이란 다음과 같이 정의된다. 한 쌍의 괄호로만 이루어진 ‘()’와 ‘[]’는 올바른 괄호열이다. 만일
www.acmicpc.net
풀이 : https://daan.tistory.com/15
백준 2504 괄호의 값
https://www.acmicpc.net/problem/2504 2504번: 괄호의 값 4개의 기호 ‘(’, ‘)’, ‘[’, ‘]’를 이용해서 만들어지는 괄호열 중에서 올바른 괄호열이란 다음과 같이 정의된다. 한 쌍의 괄호로만 이루어진
daan.tistory.com
'알고리즘 > 백준' 카테고리의 다른 글
백준 3111 검열 (0) | 2021.12.26 |
---|---|
백준 2841 외계인의 기타 연주 (0) | 2021.12.26 |
백준 4889 안정적인 문자열 (0) | 2021.12.25 |
백준 2504 괄호의 값 (0) | 2021.12.25 |
백준 1662 압축 (0) | 2021.12.25 |