N个数进栈的出栈顺序全排列及排列种数

来源:互联网 发布:windows vista 版本 编辑:程序博客网 时间:2024/06/06 19:42
     具体思路就是先列出1~N的全排列,然后再模拟入栈、出栈的过程筛选满足条件的排列。    
//Made by Xu Lizi//2013/10/24#include <iostream>#include <cstdio>#include <cstring>#include <stack>using namespace std;int a[1000];long long num = 0;bool test_stack (int n);void print(int l, int r, int n) {  if (l == r) { if (test_stack(n)) {for(int i = 0; i < n; i++) {         //输出满足条件的排列 printf("%d" , a[i]);}printf("\n");num++;}        return;      }                         //1~N个数的全排列        //来源自http://blog.csdn.net/pay_smile_yy/article/details/8250869        else {             for(int i = l; i < r; i++) {              swap (a[i], a[l]);            print (l + 1, r, n);            swap (a[i], a[l]);        }      }  };bool test_stack (int n)                     //通过模拟入栈、出栈的过程来判断满足条件 {stack <int> s;int test = 1, i;for (i = 0; i < n;) {if (test > n)return false;if (test != a[i]) {s.push(test);test++;} else {test++;i++;while (!s.empty() && s.top() == a[i]) {s.pop();i++;}}}return true;};int main (){int N;cin >> N;for (int i = 0; i < N; i++) {a[i] = i+1;}print(0, N, N);printf("%lld\n", num);return 0;}