백준 4949 c++

magicdrill·2024년 3월 18일
0

백준 문제풀이

목록 보기
169/654

백준 4949 c++

#include <iostream>
#include <vector>
#include <stack>
#include <string>

using namespace std;

void input_string(vector <string>& str);
void check_balance(vector <string> str);
bool check_string(string str);

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

	vector <string> string_list;

	input_string(string_list);
	check_balance(string_list);

	return 0;
}

void input_string(vector <string>& str)
{
	//cout << "input_string" << endl;
	string temp;

	while (1)
	{
		getline(cin, temp);
		if (temp == "." && temp.length() == 1)
		{
			break;
		}
		else
		{
			if (temp.length() <= 101 && temp.back() == '.')
			{
				str.push_back(temp);
			}
			else
			{
				;
			}
		}
	}

	return;
}

void check_balance(vector <string> str)
{
	//cout << "check_balance" << endl;
	int i;
	string temp;
	bool result;

	for (i = 0; i < str.size(); i++)
	{
		temp = str[i];
		result = check_string(temp);
		if (result == true)
		{
			cout << "yes" << "\n";
		}
		else
		{
			cout << "no" << "\n";
		}
	}

	return;
}

bool check_string(string str)
{
	//cout << "check_string" << endl;
	stack <char> temp;
	int i;

	for (i = 0; i < str.length(); i++)
	{
		if (str[i] == '(')
		{
			temp.push('(');
		}
		else if (str[i] == '[')
		{
			temp.push('[');
		}
		else
		{
			if (str[i] == ')')
			{
				if (temp.empty() != true && temp.top() == '(')
				{
					temp.pop();
				}
				else
				{
					return false;
				}
			}
			else if (str[i] == ']')
			{
				if (temp.empty() != true && temp.top() == '[')
				{
					temp.pop();
				}
				else
				{
					return false;
				}
			}
			else
			{
				;
			}
		}
	}
	if (temp.empty())
	{
		return true;
	}
	else
	{
		return false;
	}
}

0개의 댓글