SDUT 2143 数据结构实验之栈四:括号匹配

来源:互联网 发布:天下粮仓知乎 编辑:程序博客网 时间:2024/05/29 09:43

点击打开题目链接

#include <bits/stdc++.h>using namespace std;char _string[60];stack<char>S;int push_pop(char _string[]);int main(){    while(gets(_string))    {        while(!S.empty())        {            S.pop();        }//记得清空栈,连交几次wa,这就比较尴尬了        if(push_pop(_string))        {            cout << "yes" << endl;        }        else        {            cout << "no" << endl;        }    }    return 0;}int push_pop(char _string[]){    int lenth = strlen(_string);    for(int i = 0; i < lenth; i++)    {        switch(_string[i])        {          case '(':          case '[':          case '{':S.push(_string[i]);break;          case ')':if(!S.empty()&&S.top() == '(') S.pop();                   else return 0;break;          case ']':if(!S.empty()&&S.top() == '[') S.pop();                   else return 0;break;          case '}':if(!S.empty()&&S.top() == '{') S.pop();                   else return 0;break;        }    }    if(S.empty())//栈空        return 1;     return 0;}


0 0
原创粉丝点击