这个方法能拿满分,但是拿去测题解样例过不了,有没有大佬解释一下
查看原帖
这个方法能拿满分,但是拿去测题解样例过不了,有没有大佬解释一下
1084316
mayaoyuan181107楼主2025/6/26 11:28

测( [ ) ] )有问题

#include <bits/stdc++.h>
using namespace std;
#define N 1005
int tot=0;
int book[N];
struct T{
	int idx;
	char c;
}a[N],stk[N];
int main(){
	string s;
	cin >> s;
	for(int i=1;i<=s.size();i++){
		a[i].c=s[i-1];
		a[i].idx=i;
	}
	for(int i=1;i<=s.size();i++){
		if(a[i].c=='('||a[i].c=='['){
			tot++;
			stk[tot].c=a[i].c;
			stk[tot].idx=a[i].idx;
		}
		if(a[i].c==')'&&stk[tot].c=='('){
			book[stk[tot].idx]=1;
			book[a[i].idx]=1;
			tot--;
			continue;
		}
		if(a[i].c==']'&&stk[tot].c=='['){
			book[stk[tot].idx]=1;
			book[a[i].idx]=1;
			tot--;
			continue;
		}
	}
	for(int i=1;i<=s.size();i++){
		if(book[i]==1)cout << a[i].c;
		else{
			if(a[i].c=='(')
			cout << a[i].c << ')';
			if(a[i].c=='[')
			cout << a[i].c << ']';
			if(a[i].c==')')
			cout << '(' << a[i].c;
			if(a[i].c==']')
			cout << '[' << a[i].c;
		}
	}
	return 0;
}//甚为不解********
2025/6/26 11:28
加载中...