专栏文章

题解:P14172 【MX-X23-T2】括号串

P14172题解参与者 17已保存评论 19

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
19 条
当前快照
1 份
快照标识符
@minogbmx
此快照首次捕获于
2025/12/02 05:44
3 个月前
此快照最后确认于
2025/12/02 05:44
3 个月前
查看原文

P14172括号串题解

多测不清空,爆零两行泪 QwQ

(主包因为 cnt 和 top 没清空检查许久)
  • 简单模拟
  • 遇到 ( ) 匹配直接弹出栈
  • 注意到 ) (的情况只能弹出一次(cnt派上用场了 嘿嘿QwQ) -最后如果栈不为空则不可爱,反之则可爱 代码放在这里啦
CPP
#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

评论

19 条评论,欢迎与作者交流。

正在加载评论...