UVaOJ673---Parentheses Balance

来源:互联网 发布:mac ps中的3d功能在哪 编辑:程序博客网 时间:2024/06/07 05:06

673 - Parentheses Balance

Time limit: 3.000 seconds

You are given a string consisting of parentheses () and []. A string of this type is said to be correct:

(a)
if it is the empty string
(b)
if A and B are correct, AB is correct,
(c)
if A is correct, (A) and [A] is 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.

Input

The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.

Output

A sequence of Yes or No on the output file.

Sample Input

3([])(([()])))([()[]()])()

Sample Output

YesNoYes



Miguel Revilla 
2000-08-14

水题~~~~~~~~~~~~~~~~~~~~
#include <iostream>#include <stack>#include <string>using namespace std;int n;string str;enum Parentheses{   BRACKET,   PARENTHESIS,};stack<Parentheses> parentheses;bool isYes;int main(){    cin>>n;    cin.get();    while (n --)    {       while (! parentheses.empty())       {          parentheses.pop();       }       getline(cin, str);       isYes = true;       for (unsigned int i = 0; i < str.length(); i++)       {          if (str[i] == '(')          {             parentheses.push(PARENTHESIS);          }          else if (str[i] == '[')          {             parentheses.push(BRACKET);          }          else if (str[i] == ')')          {             if (parentheses.empty() || parentheses.top() != PARENTHESIS)             {                isYes = false;                break;             }             else             {                parentheses.pop();             }          }          else if (str[i] == ']')          {             if (parentheses.empty() || parentheses.top() != BRACKET)             {                isYes = false;                break;             }             else             {                parentheses.pop();             }          }       }       if (! parentheses.empty())       {          isYes = false;       }       if (isYes)       {          cout<<"Yes"<<endl;       }       else       {          cout<<"No"<<endl;       }    }    return 0;}


原创粉丝点击