70分菜鸟求助
  • 板块P1241 括号序列
  • 楼主Xyyo
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/7/16 17:36
  • 上次更新2023/10/27 20:00:39
查看原帖
70分菜鸟求助
191716
Xyyo楼主2022/7/16 17:36

可以试一下([)]的样例

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
string a,b;
int c[105];
stack<char>st;
stack<int>s;
void cyh(){
	for(int i=0;i<a.length();i++){
		if(st.size()==0);
		else{
			if((st.top()=='('&&a[i]==')')||(st.top()=='['&&a[i]==']')){
				st.pop();
				c[i]=0;
				c[s.top()]=0; 
				s.pop();
				continue;
			}
		}
		st.push(a[i]);
		s.push(i);
    }
}
void wyn(){
	for(int i=0;i<a.length();i++){
		if(c[i]==1){
			cout<<a[i]<<')';
		}
		else if(c[i]==2){
			cout<<'('<<a[i];
		}
		else if(c[i]==3){
			cout<<a[i]<<']';
		}
		else if(c[i]==4){
			cout<<'['<<a[i];
		}
		else{
			cout<<a[i];
		}
	}
}
int main()
{
	cin>>a;
	memset(c,0,sizeof(a.length()));
	for(int i=0;i<a.length();i++){
		if(a[i]=='(') c[i]=1;
		if(a[i]==')') c[i]=2;
		if(a[i]=='[') c[i]=3;
		if(a[i]==']') c[i]=4; 
	}
	cyh();
	wyn();
	return 0;
}
2022/7/16 17:36
加载中...