题解 | 括号的匹配

括号的匹配

https://www.nowcoder.com/practice/78f297134b0c4af09fb88218321c3fcc

#include <bits/stdc++.h>
using namespace std;
void solve()
{
    string s;
    cin>>s;
    stack<char>t;
    for(int i=0;i<s.size();i++)
    {
        if(t.empty())
        t.push(s[i]);
        else
        {
        if((t.top()=='('&&s[i]==')')||(t.top()=='['&&s[i]==']')||(t.top()=='{'&&s[i]=='}')||
        (t. top()=='<'&&s[i]=='>'))
            t.pop();
            else
            t.push(s[i]);
        }
    }
    if(t.empty())
    cout<<"YES"<<'\n';
    else
    cout<<"NO"<<'\n';
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 17:04
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务