您的位置:首页 > 其它

UVA 673 Parentheses Balancet

2015-09-28 16:32 225 查看
题意:判断两种括号是否匹配

如果是只有一种括号 只要保证时刻左括号数量>=右 且总的左右数量相等即可。

这题是有两种括号 因为涉及到两个括号互相嵌套影响的问题 " ([)] " 无法单独处理某一种括号。

不过我们可以用一个栈来维护 如果是左括号就入栈 右括号就看是否和栈顶左括号匹配

注意输入有空串

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<stack>
#define scnaf scanf
#define cahr char
#define bug puts("bugbugbug");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod=1000000007;
const int maxn=126+10;
const int inf=1e9;
char a[maxn];
bool ok()
{
    stack<char>stk;
    int la=strlen(a);
        for(int i=0;i<la;i++)
            if(a[i]==']'){
                if(stk.empty()||stk.top()!='[') return 0;
                stk.pop();
            }
            else if(a[i]==')') {
                if(stk.empty()||stk.top()!='(') return 0;
                stk.pop();
            }
            else stk.push(a[i]);
     return stk.empty();
}
int main()
{
    int T_T;
    scanf("%d",&T_T);
    getchar();
    while(T_T--)
    {
        gets(a);
        if(ok())puts("Yes");
        else puts("No");
    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: