티스토리 뷰

올바른 괄호

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int a[1501];
int main() {
	//freopen("input.txt", "rt", stdin);
	int i, key, cnt=0, flag=0;
	stack<char> s;
	char a[50];
	i=0;
	scanf("%s", a);
	for(i=0; a[i]!='\0'; i++){
		if(a[i]=='('){
			s.push(a[i]);
		}else {
			if(s.empty()){
				flag=1;
				printf("NO");
				break;
			}
			else s.pop();
		}
	}
	if(s.empty()) {
		if(flag==0){
			printf("YES");
		}
	}
	else {
		printf("NO");	
	}
	return 0;
}

 

기차운행

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	//freopen("input.txt", "rt", stdin);
	int n, m, i, j, key, cnt=0, flag=0;
	stack<int> s;
	scanf("%d", &n);
	vector<int> a(n+1);
	for(i=1; i<=n; i++){
		a[i]=i;
	}
	vector<char> str;
	j=1;
	for(i=1; i<=n; i++){
		scanf("%d", &m);
		s.push(m);
		str.push_back('P');
		while(1){
			if(s.empty()) break;
			if(s.top()==a[j]){
				s.pop();
				str.push_back('O');
				j++;
			} else break;
			
		}
	}
	if(!s.empty()){
		printf("impossible");
	}else {
		for(i=0; i<str.size(); i++){
			printf("%c", str[i]);
		}
	}
	
	return 0;
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함