求救
查看原帖
求救
1074812
Jared0503楼主2024/1/21 21:53

代码附上

#include<bits/stdc++.h>
using namespace std;
stack<char> op;
stack<int> num;
string s="",b="";
char a;
int i;
int main()
{
	while(cin>>a)
	{
		s=s+a+' ',i++;
		if(a=='['||a=='(') op.push(a),num.push(i); //左括号无匹配,故将其入op和num(下标)的栈 
		else
		{
			if(op.top()=='('||op.top()=='[') op.pop(),num.pop(); //右括号有匹配,故将op和num(下标)栈顶弹出 
			else op.push(a),num.push(i); //右括号无匹配,故将其入栈 
		}
	}
	//0123
	//([()
	
	//op:([ 
	//num:01
	//(num.top()+1)*2-1 
	//s:( [ ( )
	
	//下标3加],下标1加) 
	while(!op.empty())
	{
		cout<<op.top()<<' '<<num.top()<<endl;
		op.pop(),num.pop();
//		a=op.top();
//		if(a=='[') s[num.top()*2-1]=']';
//		else s[num.top()*2-1]=')';
//		op.pop(),num.pop();
	}
	for(int i=0;i<s.size();i++) if(s[i]!=' ') b+=s[i];
	cout<<b;
}
2024/1/21 21:53
加载中...