给定入栈顺序,求解出栈顺序
2015-08-05 10:45
155 查看
#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; }
相关文章推荐
- access 注释之 ColumnHistory错误问题记录
- 在Ubuntu中USB连接手机调试
- (FFOS Gecko & Gaia) OTA - Do real check
- 七牛是如何搞定每天500亿条日志的
- 剑指offer面试题15——链表中倒数第k个结点
- JavaScript自增、自减运算符与表达式语法
- QT实现一个简单的计算器
- SQL2005、SQL2008允许远程连接的配置说明(附配置图)
- 自己动手构建表单验证功能【iOS】
- Android Download机制详解
- Construct Binary Tree from Inorder and Postorder Traversal
- poj 3620 Avoid The Lakes
- JavaScript Dom编程艺术 1 - 5章个人总结(共12章)
- LeetCode237——Delete Node in a Linked List
- 解决myeclipse中不能自动装箱拆箱问题
- 常用Git命令及技巧总结
- git rebase简介(基本篇)
- assert()函数用法总结
- 开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
- Hdu 5348 MZL's endless loop (dfs)