括号匹配

来源:互联网 发布:simulink 知乎 编辑:程序博客网 时间:2024/05/22 10:24
#include<map>#include<stack>#include<string>#include<cstdio>#include<iostream>using namespace std;int a[1000];bool check(string st){    map<char,int>mp;    mp['(']=0;mp[')']=1;    mp['[']=2;mp[']']=3;    mp['{']=4;mp['}']=5;    int A=0;    for (int i=0;i<st.size();i++){        if (st[i]!='('&&st[i]!=')'&&            st[i]!='['&&st[i]!=']'&&            st[i]!='{'&&st[i]!='}')continue;        a[++A]=mp[st[i]];    }    stack<int>s;    while (!s.empty())s.pop();    for (int i=1;i<=A;i++){        if (!(a[i]&1))s.push(a[i]);        else {            if (s.empty())return 0;            int x=s.top(); s.pop();            if (x^a[i]!=1)return 0;        }    }    return s.empty();}//=======cww=2016.2.29=9:12=========int main(){    string st;    while (cin>>st){        if (check(st))puts("yes");        else puts("no");    }    return 0;}
0 0
原创粉丝点击