题解:P14172 【MX-X23-T2】括号串
P14172括号串题解
多测不清空,爆零两行泪 QwQ
(主包因为 cnt 和 top 没清空检查许久)
- 简单模拟
- 遇到
( )匹配直接弹出栈 - 注意到
) (的情况只能弹出一次(cnt派上用场了 嘿嘿QwQ) -最后如果栈不为空则不可爱,反之则可爱 代码放在这里啦#include<bits/stdc++.h> #define ll long long using namespace std; const int N=1e5+10; int i,t,n,cnt,top; char ch,s[N]; int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cin>>t; while(t--) { top=0,cnt=0; cin>>n; for(i=1;i<=n;i++) { cin>>ch; if(ch==')'&&s[top]=='(') { --top; continue; } else if(ch=='('&&s[top]==')'&&!cnt){ --top;++cnt; continue; } s[++top]=ch; } if(top) puts("No"); else puts("Yes"); } return 0; }码风也和题目一样可爱呢QwQ