UVa OJ Parentheses Balance 673 括号平衡

    xiaoxiao2021-03-25  185

    UVa OJ Parentheses Balance 673 括号平衡

    解题思路:

    栈。AC代码如下,仅供参考。

    #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<stack> #define MAXN 128+10 using namespace std; int main(){ #ifdef LOCAL freopen("673_input.txt","r",stdin); freopen("673_output.txt","w",stdout); #endif string s; int n; while(scanf("%d\n",&n)==1){ for(int i=0;i<n;i++){ getline(cin,s); stack<char>p; int flag=1; for(int j=0;j<s.size();j++){ if(s[j]=='('||s[j]=='[') p.push(s[j]); else if(!p.empty()&&s[j]==')'&&p.top()=='(') p.pop(); else if(!p.empty()&&s[j]==']'&&p.top()=='[') p.pop(); else{ flag=0; break; } } printf("%s\n",flag&&p.empty()?"Yes":"No"); } } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-4918.html

    最新回复(0)