uva673

来源:互联网 发布:mac os系统dmg镜像 编辑:程序博客网 时间:2024/05/17 03:17

简单括号匹配问题,注意空串也是符合要求,还有就是要有空串,则必须要gets()或者getline。不能用scanf。(此处WAn遍)

//  Accepted C++11  0.098#include <cstdio>#include <cstring>#include <iostream>#include <stack>using namespace std;const int maxn = 1000+10;int main(){  int n;  char data[maxn];  while(scanf("%d", &n) != EOF){    getchar();    while(n --){        memset(data, 0, sizeof(data));        gets(data);        //if(strlen(data) == 0) printf("Yes\n");        //else {          stack<char> s;          while(!s.empty()) s.pop();          int flag = 1;          for(int i = 0; i < strlen(data); i ++){              if(data[i] == '(' || data[i] == '[')                  s.push(data[i]);              else if(data[i] == ')' || data[i] == ']'){                if(data[i] == ')') {                  if(!s.empty() && s.top() == '(') s.pop();                  else { flag = 0; break; }                }                else if(data[i] == ']'){                  if(!s.empty() && s.top() == '[') s.pop();                  else { flag = 0; break; }                }              }          }          if(s.empty() && flag == 1) printf("Yes\n");          else printf("No\n");        }    }  //}  return 0;}


 

0 0