您的位置:首页 > 其它

UVa 673 - Parentheses Balance

2013-04-15 14:53 295 查看
  用栈进行模拟就行了,不过要考虑空行的情况,该开始就没考虑数据有空行的情况,认为不可能,WA了一次,果然没什么理所当然啊。

  代码如下:

View Code

#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;

const int maxn = 150;

int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n;
char data[150];
stack<char> s;
scanf("%d", &n);
getchar();
while(n--)
{
fgets(data, maxn, stdin);
int len = strlen(data);
if(len == 1)
{
printf("Yes\n");
continue;
}
len--;
while(!s.empty())   s.pop();
for(int i = 0; i < len; i++)
{
if(!s.empty() && ((data[i] == ')' && s.top() == '(') || (data[i] == ']' && s.top() == '[')))
s.pop();
else s.push(data[i]);
}
if(s.empty())   printf("Yes\n");
else printf("No\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: