吴晓阳
发布于 2023-03-21 / 300 阅读
0

打印栈的出栈序列

#include<bits/stdc++.h> 
using namespace std;
vector<int> ppp;
int n;
//vector<int> temp;
void dfs(int index,stack<int> s,vector<int> v,int &num){
	for(int i=s.size();i>=0;i--){
		stack<int> stemp(s);
		vector<int> vtemp(v);
		for(int j=1;j<=i;j++){
			int top=stemp.top();
			stemp.pop();
			vtemp.push_back(top);
		}
		stemp.push(ppp[index]);
		if(index==n){
			num++;
			for(auto it=vtemp.begin();it!=vtemp.end();it++){
				cout<<*it;
			}
			while(stemp.empty()==false){
				cout<<stemp.top();
				stemp.pop();
			}
			cout<<endl;
		}else{
			dfs(index+1,stemp,vtemp,num);
		}
	}
}
int main()
{
	//freopen("a.in","r",stdin);
	while(scanf("%d",&n)!=EOF){
		ppp.clear();
		ppp.push_back(0);
		for(int i=1;i<=n;i++){
			ppp.push_back(i);
		}
		int num=0;
		stack<int> s;
		vector<int> v;
		dfs(1,s,v,num);
		cout<<num<<endl;
	}
	return 0;
}