ÀڷᱸÁ¶ °úÁ¦ÁßÀε¥ ºñÀü°øÀÚ¶ó¼ ³Ê¹« ¾î·Æ°í ¹°¾îº¼ ¶§µµ ¾ø³×¿ä ¤Ð¤Ð ½ºÅÃÀ» ÀÌ¿ëÇÑ °ýÈ£°Ë»ç ÇÁ·Î±×·¥À» ¸¸µå´Â°Çµ¥ ÁÖ¼®, ÀÛÀº µû¿ÈÇ¥, Å« µû¿ÈÇ¥ ¾È¿¡ÀÖ´Â °ýÈ£´Â ¹«½ÃÇÏ¸é¼ °Ë»çÇÏ´Â ÇÁ·Î±×·¥Àε¥ ¾î¶»°Ô ó¸®ÇØ¾ß ÇÒÁö ¸ð¸£°Ú¾î¿ä
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
inline void error(const char* message) {
printf("%s\n", message);
exit(1);
}
const int MAX_STACK_SIZE = 20;
class ArrayStack {
int top;
int data[MAX_STACK_SIZE] = { 0, };
public:
ArrayStack() { top = -1; ; }
~ArrayStack() {}
bool isEmpty() { return top == -1; }
bool isFull() { return top == MAX_STACK_SIZE - 1; }
void push(int e) {
if (isFull()) error("½ºÅà Æ÷È ¿¡·¯");
data[++top] = e;
}
int pop() {
if (isEmpty()) error("½ºÅà °ø¹é ¿¡·¯");
return data[top--];
}
int peek() {
if (isEmpty()) error("½ºÅà °ø¹é ¿¡·¯");
return data[top];
}
void display() {
printf("[½ºÅà Ç׸ñÀÇ ¼ö=%2d] ==> ", top + 1);
for (int i = 0; i <= top; i++)
printf("<%2d>", data[i]);
printf("\n");
}
};
bool checkMatching(const string& s) {
int nLine = 1;
int bracket = 0;
int nChar = 1;
bool quotation = false, bigQuot = false;
bool note = false;
ArrayStack stack;
for (int i = 0; i < s.size(); i++) {
char ch = s[i];
nChar++;
if (ch == '\n') nLine++;
if (ch == '[' || ch == '{' || ch == '(')
stack.push(ch);
else if (ch == ']' || ch == '}' || ch == ')') {
if (stack.isEmpty()) return false;
else {
int prev = stack.pop();
bracket++;
if ((ch == ']' && prev != '[') ||
(ch == '}' && prev != '{') ||
(ch == ')' && prev != '(')) return false;
} //else
} //else if
} //for
if (!stack.isEmpty())
printf("error : ¶óÀμö %d ÀÏÄ¡°ýÈ£¼ö %d ¹®ÀÚ¼ö%d\n", nLine-1, bracket, nChar);
else
printf("OK : ¶óÀμö %dÀÏÄ¡°ýÈ£¼ö %d ¹®ÀÚ¼ö%d\n", nLine-1, bracket, nChar);
return stack.isEmpty();
} //function
int main() {
std::string Str, temp;
while (true) {
std::getline(std::cin, temp);
if (temp == "EOF")break;
Str.append(temp);
Str.append("\n");
std::cin.clear();
}
checkMatching(Str);
return 0;
}