AC的不是我的
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 1001 int main() { char s[N], a[N]; while(gets(s)) { int len,i,top = 0; len= strlen(s); for(i = 0; i < len; i++) { // if(s[i] == '(' || s[i] == '[' || s[i] == '{') { a[++top] = s[i]; } else if(s[i] == ')') { if(a[top] == '(') top--; else { top++; break; } } else if(s[i] == ']') { if(a[top] == '[') top--; else { top++; break; } } else if(s[i] == '}') { if(a[top] == '{') top--; else { top++; break; } } } if(top!=0) printf("no\n"); else printf("yes\n"); } return 0; }
我的没过:
#include <bits/stdc++.h> using namespace std; int main() { char a[55]; while (scanf("%s", a) != EOF) { stack<char> b; int len = strlen(a); int flag = 0; for (int i = 0; i < len; i++) { if (a[i] == '(' || a[i] == '[' || a[i] == '{') b.push(a[i]); // if (!b.empty()) // { // if (b.top() == '(' && a[i] == ')') // b.pop(); // else if (b.top() == '{' && a[i] == '}') // b.pop(); // else if (b.top() == '[' && a[i] == ']') // b.pop(); // } if (!b.empty()) { if (a[i] == ')') if (b.top() == '(') b.pop(); else { flag = 1; break; } else if (a[i] == ']') if (b.top() == '[') b.pop(); else { flag = 1; break; } else if (a[i] == '}') if (b.top() == '{') b.pop(); else { flag = 1; break; } } } if (flag) { printf("no\n"); } else { if (b.empty()) { printf("yes\n"); } else printf("no\n"); } } return 0; }