2134-数据结构实验之栈与队列四:括号匹配

来源:互联网 发布:怎么检查网络被盗用 编辑:程序博客网 时间:2024/05/17 18:16
#include <bits/stdc++.h>#define MAX 1000000using namespace std;char st[100];bool match(char a, char b){    if( (a == '(' && b == ')') ||        (a == '{' && b == '}') ||        (a == '[' && b == ']') )        return true;    else        return false;}int main(){    while(gets(st))    {        int t = 1;        stack<char>Q;        for(int i = 0; st[i]; i++)        {            if(st[i] == '(' || st[i] == '{' || st[i] == '[')            {                Q.push(st[i]);            }            else if(st[i] == ')' || st[i] == '}' || st[i] == ']')            {                if(Q.empty() || !match(Q.top(),st[i]))                {                    t = 0;                    break;                }                else if(match(Q.top(),st[i]))                Q.pop();            }        }        if(Q.empty() && t)        {            cout << "yes" << endl;        }        else        {            cout << "no" << endl;        }    }}
阅读全文
0 0
原创粉丝点击