인프런 커뮤니티 질문&답변
답변 2
0
김혁수
질문자
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;
int main() {
int n;
scanf("%d",&n);
vector<int> a(n+2);
for (int i = 1; i <= n; i++){
scanf("%d",&a[i]);
}
stack<int> s;
vector<char> c;
s.push(a[1]);
c.push_back('P');
int i = 2;
int j = 1;
while(i<=n || j <= n ){
if(s.top() == j){
s.pop();
c.push_back('O');
j++;
}
else {
s.push(a[i]);
c.push_back('P');
i++;
}
}
if(!s.empty()) printf("impossible");
else {
for (int k = 0; k<c.size();k++) printf("%c",c[k]);
}
return 0;
}
0





