爆搜了一下,没想到AC了,可能数据有点弱吧
http://www.tyvj.cn/p/1336
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
int check(int *a,int n){
// cout<<n<<endl;
stack<int>s;
int t,i,j;
// for(int i=0;i<n;i++){
// cout<<a[i];
// }
for(i=0,j=1;i<n&&j<=n;){
// cout<<a[i]<<' '<<j<<endl;
if(a[i]==j){
j++;
i++;
continue;
}
else if(!s.empty()){
t=s.top();
if(t==a[i]){
s.pop();
i++;
continue;
}
else{
s.push(j);
j++;
}
}
else{
s.push(j);
j++;
}
}
if(i==n&&j==n+1&&s.empty()){
return 1;
}
if(i<n&&!s.empty()){
while(!s.empty()){
int t=s.top();
if(t==a[i++]){
s.pop();
continue;
}
else{
return 0;
}
}
return 1;
}
// cout<<i<<' '<<j<<' '<<(s.empty())<<endl;
return 0;
}
using namespace std;
int main(){
int n;
int a[100];
int cnt=0;
cin>>n;
for(int i=0;i<n;i++){
a[i]=i+1;
}
do{
if(check(a,n)){
// cout<<'d'<<endl;
for(int i=0;i<n;i++){
printf("%d",a[i]);
}
cout<<endl;
cnt++;
}
if(cnt==20){
break;
}
}while(next_permutation(a,a+n));
}