括号匹配

#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;
}
posted @ 2016-03-14 16:52  伟大的蚊子  阅读(128)  评论(0编辑  收藏  举报