Programming/Algorithm
[Algorithm] C++ 올바른 괄호(Stack)
100winone
2020. 1. 2. 23:56
입력예제 1
(()(()))(()
출력예제 1
NO
입력예제 2
()()(()())
출력예제 2
YES
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
int main() {
stack <char> s;
char a[50];
int i, flag = 1;
scanf("%s", &a);
for (i = 0; a[i] != '\0'; i++) {
if (a[i] == '(') s.push(a[i]);
else {
if (s.empty()) {
printf("NO\n");
flag = 0;
break;
}
else s.pop();
}
}
if (s.empty() && flag == 1) printf("YES\n");
else if (!s.empty() && flag == 1) printf("NO\n");
return 0;
}