字符串匹配问题

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
int a[10001] = {0}, b[10001] = {0};
char s[10001];
void work(int a[])
{
    for(int i = 0;i < strlen(s);i++)
    {
        if(s[i] == '{') a[i+1] = 1;
        if(s[i] == '[') a[i+1] = 2;
        if(s[i] == '(') a[i+1] = 3;
        if(s[i] == '<') a[i+1] = 4;
        if(s[i] == '>') a[i+1] = 5;
        if(s[i] == ')') a[i+1] = 6;
        if(s[i] == ']') a[i+1] = 7;
        if(s[i] == '}') a[i+1] = 8;
    }
    
 }
// 将括号转换为数字,便于比较级别 
int main()
{
    int n, t = 0, m;
    scanf("%d", &n);
    getchar();
//    读回车,不然读n的时候就会输出  
    for(int i = 1;i <= n;i++)
    {
        gets(s);
        work(a);
        for(int j = 1;j <= strlen(s);j++)
        {
            if(a[j] <= 4)
            {
                if(a[j] >= b[t]) b[++t] = a[j];
                else break;
            }
//        如果是左括号且符合括号级别顺序,左括号进栈;否则匹配失败 
            if(a[j] >= 5)
            {
                if(a[j] + b[t] == 9) t--;
                else t++;
            }
//        如果是右括号则进栈,匹配成功后出栈
        }
         if(t == 0) printf("YES");
         else printf("NO");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/thx666/p/8324250.html