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 32 33 34 35 36 37 38 39 40 41 42 43 | #include <stdio.h> #include <iostream> #include <string> using namespace std; int main() { while (1) { string str; getline(cin, str); if (str == ".") break; char stk[105] = { 0 }; int curIdx = -1; int len = str.size(); for (int i = 0; i < len; i++) { if ((str.at(i) == '[') || (str.at(i) == ']') || (str.at(i) == '(') || (str.at(i) == ')')) { if (curIdx == -1) { stk[0] = str.at(i); curIdx++; } else { if ((stk[curIdx]=='[' && str.at(i)==']')||(stk[curIdx] == '(' && str.at(i) == ')')) { stk[curIdx] = 0; curIdx--; } else { stk[++curIdx] = str.at(i); } } } } if (curIdx == -1) printf("yes\n"); else printf("no\n", curIdx); } } | cs |
스텍을 이용한 문제였다.
eof 를 쓰지 않아도 됐었다.
매칭을 시키는 부분에서 완전 잘못 생각해서 헤맸다.
'알고리즘 > 문자와 문자열' 카테고리의 다른 글
10545 뚜기뚜기메뚜기 (0) | 2020.01.06 |
---|---|
3077 임진왜란 (0) | 2020.01.06 |
9933 민균이의 비밀번호 (0) | 2020.01.06 |
3986 좋은 단어 (0) | 2020.01.06 |
10551 STROJOPIS (0) | 2020.01.06 |