UVA - 673
| Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
| Parentheses Balance |
You are given a string consisting of parentheses () and []. A string of this type is said to be correct:
Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.
3 ([]) (([()]))) ([()[]()])()
Yes No Yes
Source
简单栈的应用。。判断括号是否匹配
没看到空字符,WA了两次。。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
getchar();
while(n--)
{
char a[130];
stack<char> s;
gets(a);
int flag = 0;
for(int i=0; a[i] != '\0'; i++)
{
if(a[i] == '(' || a[i] == '[') s.push(a[i]);
else if(a[i] == ')')
{
if(!s.empty() && s.top() == '(') s.pop();
else { flag = 1; break; }
}
else if(a[i] == ']')
{
if(!s.empty() && s.top() == '[') s.pop();
else { flag = 1; break; }
}
}
if(flag || !s.empty()) printf("No\n");
else printf("Yes\n");
}
}
return 0;
}
UVA - 673 - Parentheses Balance (栈的应用!)
原文地址:http://blog.csdn.net/u014355480/article/details/42008865