개인공부용123 프로그래밍 블로그
[백준]boj4949 본문
코드만
#include <iostream>
#include <string.h>
using namespace std;
#define MAX 200
char stack[MAX];
int top;
bool isEmpty() {
return top == 0 ? true : false;
}
bool isFull() {
return top == MAX ? true : false;
}
void push(char data) {
if (isFull()) return;
stack[top++] = data;
}
char pop() {
if (isEmpty()) return -1;
char ret = stack[top - 1];
stack[--top] = 0;
return ret;
}
string validation(char* str) {
int len = strlen(str);
char c;
top = 0;
for (int i = 0; i < len; ++i) {
if (str[i] == '(' || str[i] == '[') {
push(str[i]);
}
else if (str[i] == ')') {
c = pop();
if (c != '(') return "no";
}
else if (str[i] == ']') {
c = pop();
if (c != '[') return "no";
}
}
if (top != 0) return "no";
else return "yes";
}
int main() {
freopen("test.txt", "r", stdin);
while (1) {
char str[107];
cin.getline(str, 107);
if (!strcmp(str, ".")) break;
cout << validation(str) << endl;;
}
return 0;
}