题解 | #火车进站#

火车进站

https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109

#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;

bool isallowed(int a[], int n,int b[]) {
	stack<int>s;
	int x = 0;
	for (int i = 0; i < n; i++) {
		if (a[x] != b[i])s.push(b[i]);
		if (a[x] == b[i]) {
			x++;
		}while (!s.empty() && s.top() == a[x]) {
			s.pop();
			x++;
		}

	}if (s.empty()) {
		return true;
	}
	else {
		return false;
	}


}

int main() {
	int n;
	cin >> n;
	int a[11];
	int b[11];
	for (int i = 0; i < n; i++) {
		cin >> b[i];
	}for (int i = 0; i < n; i++)a[i] = b[i];
	sort(a, a + n);
	do {
		if (isallowed(a, n, b)) {
			for (int i = 0; i < n; i++) {
				cout << a[i] << " ";
			}cout << endl;
		}
		
	} while (next_permutation(a, a + n));


	return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务