#include "bits/stdc++.h"
using namespace std;
stack<char> st, st1;
deque<char> d, d1, d2;
int len;
string a,b;
void dfs(int in,int pop){ //in表示 入栈的次数 pop表示出栈的次数
if(in == len && pop == len){ //当入栈和出栈都完成一次的时候就输出
d1 = d;
while(!d1.empty()){
cout<<d1.front();
d1.pop_front();
}
cout<<endl;
}
if(in < len){ //入栈操作
st.push(a[in]); //当前字符进栈
dfs(in+1,pop); //进行下一次的操作
st.pop(); //恢复刚刚的入站字符,以便进行下一次操作
}
if(pop < in && pop < len ){ //出栈操作
d.push_back(st.top());
st.pop(); //当前字符出栈
dfs(in,pop+1); //搜索下一位置
st.push(d.back()); // 恢复刚刚的出站字符,以便进行下一次操作
d.pop_back();
}
}
int main(){
while(cin>>a){//因为都是先入栈再出栈,所以出栈的元素一定是之前就有的,而且是从栈顶开始出栈的,所以出栈顺序一定是合法的
len = a.length();
cout<<"["<<endl;
dfs(0,0);
cout<<"]"<<endl;
}
return 0;
}
史上最清晰的求一个序列的所有出栈顺序
最新推荐文章于 2024-08-06 20:40:43 发布