分析:很水的一道题,就是用栈来看看是不是匹配就好了,只是最后没有判断栈是否为空而WA了一个点,以后做题要注意了.
#include <bits/stdc++.h> using namespace std; stack<int> ss; char s[100010]; int sizee; bool flag = true; int main() { freopen("a.in","r",stdin); freopen("a.out","w",stdout); scanf("%s",s + 1); sizee = strlen(s + 1); for (int i = 1; i <= sizee; i++) { if (s[i] == '(' || s[i] == ')' || s[i] == '['|| s[i] == ']' || s[i] == '{'||s[i] == '}') { if (s[i] == '(' || s[i] == '['|| s[i] == '{') { if (s[i] == '(') ss.push(1); else if (s[i] == '[') ss.push(2); else if (s[i] == '{') ss.push(3); } else { int t = ss.top(); ss.pop(); if (s[i] == ')' && t != 1) { flag = false; break; } else if (s[i] == ']' && t != 2) { flag = false; break; } else if (s[i] == '}' && t != 3) { flag = false; break; } } } } if (flag && ss.empty()) printf("OK "); else printf("Wrong "); return 0; }