백준 9012 c++

magicdrill·2024년 3월 20일

백준 문제풀이

목록 보기
182/673

백준 9012 c++

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

using namespace std;

int input(int lower, int upper);
void input_testcase(vector <string>& testcase, int T);
void check_result(vector <string> testcase);
bool check_vps(string str);

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

	int T;
	vector <string> testcase;

	T = input(1, 1000000);
	input_testcase(testcase, T);
	check_result(testcase);

	return 0;
}

int input(int lower, int upper)
{
	//cout << "input" << endl;
	int A;

	while (1)
	{
		cin >> A;
		if (A >= lower && A <= upper)
		{
			break;
		}
		else
		{
			;
		}
	}

	return A;
}

void input_testcase(vector <string> &testcase, int T)
{
	//cout << "input_testcase" << endl;
	int i;
	string temp;

	for (i = 0; i < T; i++)
	{
		cin >> temp;
		if (temp.length() >= 2 && temp.length() <= 50)
		{
			testcase.push_back(temp);
		}
		else
		{
			i--;
		}
	}

	return;
}

void check_result(vector <string> testcase)
{
	//cout << "check_result" << endl;
	string temp_str;
	int i;
	bool result;

	for (i = 0; i < testcase.size(); i++)
	{
		temp_str = testcase[i];
		result = check_vps(temp_str);
		if (result == true)
		{
			cout << "YES" << "\n";
		}
		else
		{
			cout << "NO" << "\n";
		}
	}

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

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

0개의 댓글