首页 > 其他 > 详细

【UVa 673】Parentheses Balance

时间:2015-10-04 09:55:52      阅读:236      评论:0      收藏:0      [点我收藏+]
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

 Status

Description

技术分享
 

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, () and [] 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 

Yes
No
Yes

 

 


Miguel Revilla
2000-08-14
 
用一个栈。遇到‘(’或‘[’入栈,遇到‘)’或‘]’出栈,然后各种判断。
#include<cstdio>
#include<cstring>
#include<stack>

using namespace std;

stack<char> st;
char s[1000];

bool check()
{
    for (int i = 0; i < strlen(s) - 1; ++i)
        if (s[i] == ( || s[i] == [) st.push(s[i]);
        else if (st.empty()) return false;
            else if ((st.top() == ( && s[i] == )) || (st.top() == [ && s[i] == ])) st.pop();
                else return false;
    return st.empty();
}

int main()
{
    int Test;
    scanf("%d", &Test);
    getchar();
    while (Test--)
    {
        fgets(s, 1000, stdin);
        while (!st.empty()) st.pop();
        if (check()) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

 

【UVa 673】Parentheses Balance

原文:http://www.cnblogs.com/albert7xie/p/4854149.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!