UVA 673 Parentheses Balancet

来源:互联网 发布:餐车淘宝客服接待用语 编辑:程序博客网 时间:2024/05/29 17:50

题意:判断两种括号是否匹配

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

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

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

注意输入有空串

#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");    }}


0 0
原创粉丝点击