蒟蒻代码有Bug求助,码风良好
查看原帖
蒟蒻代码有Bug求助,码风良好
773234
taqingqiu楼主2024/8/10 09:39

a为输入的字符串,q为栈

#include <iostream>
#include <queue>
using namespace std;
int t;
string a;
queue<int>q;
int main () {
	cin>>t;
	for (int i=1;i<=t;i++) {
		cin>>a;
		for (int j=0;j<a.size();j++) {
			if ( (q.front()=='('&&a[j]==')') || (q.front()=='['&&a[j]==']')) {
				q.pop();
			}
			else {
				q.push(a[j]);
			}
		}
		if (!q.empty()) {
			cout<<"NO"<<endl;
			for (int j=1;j<=q.size();j++) {
				q.pop();
			}
		}
		else {
			cout<<"YES"<<endl;
		}
	}
}
2024/8/10 09:39
加载中...