문제접근
놓쳤던 부분
•
if (s.empty() || s.top() != '(') 이 조건문에서 if (s.top() != '(') || s.empty()) 이런식으로 스택이 비어있는지 먼저 확인하지 않고 top으로 접근해서 segmentation 오류 발생
코드
2024 KB
0 ms
#include <iostream>
#include <stack>
std::string str;
std::stack<char> s;
long long answer = 0;
bool flag = false;
void input_setting()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
}
void input()
{
	std::cin >> str;
}
void solution()
{
	int len = str.size();
	int tmp = 1;
	for (int i = 0; i < len; i++)
	{
		if (str[i] == '(')
		{
			tmp *= 2;
			s.push('(');
		}
		else if (str[i] == '[')
		{
			tmp *= 3;
			s.push('[');
		}
		else if (str[i] == ')')
		{
			if (s.empty() || s.top() != '(')
			{
				flag = true;
				break ;
			}
			if (str[i - 1] == '(')
				answer += tmp;
			s.pop();
			tmp /= 2;
		}
		else if (str[i] == ']')
		{
			if (s.empty() || s.top() != '[')
			{
				flag = true;
				break ;
			}
			if (str[i - 1] == '[')
				answer += tmp;
			s.pop();
			tmp /= 3;
		}
	}
}
void print()
{
	if (flag || !s.empty())
		std::cout << "0";
	else
		std::cout << answer;
}
int main(void)
{
	input_setting();
	input();
	solution();
	print();
	return (0);
}
C++
복사

