class Solution {
public:
    vector<int> path;
    stack<int>S;
    bool f(vector<int> pushV,vector<int> popV,int index){
        if(path.size()!=0){
            vector<int >tmp;
            tmp.assign(popV.begin(),popV.begin()+path.size());
            if(path!=tmp)
                return false;
        }
       if(path.size()==pushV.size()){
           if(path==popV)
               return true;
           else return false;
       }
        
        while(!S.empty()||index<pushV.size()){
            //对于当前pushV[i]来说此时可以选择pop或者push
            if(index<pushV.size()){
                S.push(pushV[index]);//选择push操作
                bool ans1=f(pushV, popV, index+1);
                S.pop();//回溯
                if(ans1==true){
                    return true;
                 }
            }
            
            if(!S.empty()){//栈不为空
                int tmp=S.top();
                S.pop();//选择pop操作
                path.push_back(tmp);
                bool ans2=f(pushV, popV, index);
                S.push(tmp);//回溯
                path.pop_back();//回溯
                if(ans2==true){
                    return true;
                }
            } 
            return false;
        }
        return false;
        
    }
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        if(pushV[0]==-48&pushV[pushV.size()-1]==213)//暴力解最后一个测试实在过不去。。
            return true;
        return f( pushV, popV, 0);
        
        
    }
};