Programming/Algorithm

[Algorithm] C++ 기차운행(stack 응용)

100winone 2020. 1. 3. 15:11

입력예제 1

2 1 3

 

출력예제 1

PPOOPO

 

 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<stack>

using namespace std;

int main() {
	stack<int> s;
	int n, m, i, j = 1;
	scanf("%d", &n);
	vector<char> str; // push back으로
	for (i = 1; i <= n; i++) {
		scanf("%d", &m);
		s.push(m);
		str.push_back('P');
		while (1) {
			if (s.empty()) break;
			if (j == s.top()) {
				s.pop();
				j++;
				str.push_back('O');
			}
			else {
				break;
			}
		}
	}
	if (!s.empty()) printf("impossible\n");
	else {
		for (i = 0; i < str.size(); i++) {
			printf("%c", str[i]);
		}
	}
	return 0;
}