给定入栈顺序,求解出栈顺序

来源:互联网 发布:电气工程制图软件 编辑:程序博客网 时间:2024/05/17 10:03
#include <iostream>#include <vector>using namespace std;void finn(const int n,int cur,vector<int>&stac,vector<int>&outL,int&count){    if((int)outL.size ()==n)//终点    {        //如果需要对应的出栈顺序那么outL中即为所求        count++;    }    if(cur!=n+1)//入栈    {        //操作        stac.push_back(cur);        //进入下层        finn(n,cur+1,stac,outL,count);        //恢复        stac.pop_back ();    }    if(!stac.empty())//出栈    {        //留底        int temp=stac[(int)stac.size ()-1];        //操作        outL.push_back (temp);        stac.pop_back ();        //进入下层        finn(n,cur,stac,outL,count);        //恢复        outL.pop_back ();        stac.push_back (temp);    }}int f(const int n){    vector<int> stac;    vector<int> outL;    int count=0;    finn(n,1,stac,outL,count);    return count;}main(){    int n;    cout<<"n=";    cin>>n;    cout<<"共"<<f(n)<<"个"<<endl;}  

0 0
原创粉丝点击