uva673

https://vjudge.net/problem/UVA-673

一开试没想到用栈,以为要用递归,递归写完之后老是WA,可能这几天看递归看的太多了,中毒了

想到用栈就简单了,要注意对空串的处理

我们用fges接受空串时,它里面只有一个\n字符,这个要注意下

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int main()
{
	int T;
	cin >> T; getchar();
	while (T--)
	{
		stack<char> stc;
		char s[300];
		fgets(s, 300, stdin);
		for (int i = 0; unsigned(i) <strlen(s); i++) {
			if (s[i] == '[' || s[i] == '(') stc.push(s[i]);
			else 
			{
				if (stc.empty()) { stc.push(s[i]); break;}
				char ch = stc.top(); 
				if ((ch == '('&&s[i] == ')') || (ch == '[' && s[i] == ']')) stc.pop();
				else break;
			}
		}
		if (!stc.empty()) cout << "No\n";
		else cout << "Yes\n";

	}
}

猜你喜欢

转载自blog.csdn.net/qq_41776911/article/details/81076310