View Code of Problem 3913

#include<iostream>
#include<string>
#include<algorithm>
#include<stack>

using namespace std;

//pop前记得判断是否为空
//stack中只要有值,必然为'(',故不需要判断stk.top() == '('

int main() {
	int t;
	string s;
	cin >> t;
	while (t--)
	{
		cin >> s;
		if (s.length() % 2 != 0)
			cout << "No" << endl;
		else
		{
			bool judge = true;
			stack<char> stk;
			for (int i = 0; i < s.length(); i++)
			{
				if (s[i] == '(')
					stk.push(s[i]);
				else
				{
					if (!stk.empty())
						stk.pop();
					else
					{
						judge = false;
						break;
					}
				}
			}
			if (!stk.empty())
				judge = false;
			if (judge)
				cout << "Yes" << endl;
			else
				cout << "No" << endl;
		}

	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3913