52分RE求救
查看原帖
52分RE求救
481521
Qiuziyi楼主2024/1/18 15:32
#include<bits/stdc++.h>
using namespace std;
string S;
int len;
char ans[110];
stack<char> s;
stack<int> x;
int main(){
	cin>>S;
	len=S.length();
	for(int i=0;i<len;i++){
		if(S[i]=='(' || S[i]=='['){
			s.push(S[i]),x.push(i);
			if(S[i]=='(') ans[i]=')';
			else ans[i]=']';
		}
		else{
			char tmp;
			if(S[i]==')') tmp='(';
			else tmp='[';
			if(s.top()==tmp){
				ans[x.top()]=' ',ans[i]=' ';
				s.pop(),x.pop();
			}else ans[i]=tmp;
		}
	}
	for(int i=0;i<len;i++){
		if(ans[i]==' ') cout<<S[i];
		else if(ans[i]=='(' || ans[i]=='[') cout<<ans[i]<<S[i];
		else cout<<S[i]<<ans[i];
	}
	return 0;
}

是类似题解第一篇的做法 TAT

2024/1/18 15:32
加载中...