NYOJ-2 括号匹配

来源:互联网 发布:百度云盘mac不可用 编辑:程序博客网 时间:2024/05/16 12:37

source link

1.tips

栈应用的经典问题。对每个准备入栈元素判断他是否和栈顶元素匹配即可。

2.code

#include <iostream>#include <string>#include <cstring>#include <cmath>#include <algorithm>#include <stack>using namespace std;int main(){    int t;    char s[10010];    cin>>t;    while(t--)    {        cin>>s;        stack<char> S;        char c;        for(int i=0;s[i];i++)        {            if(!S.empty()){                c = S.top();                if(c=='[' && s[i]==']')                    S.pop();                else if(c=='(' && s[i]==')')                    S.pop();                else                    S.push(s[i]);            }            else{                S.push(s[i]);            }        }        if(S.empty())            cout<<"Yes"<<endl;        else            cout<<"No"<<endl;    }    return 0;}
0 0
原创粉丝点击