zoukankan      html  css  js  c++  java
  • 字符串匹配问题

    #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;
    }
  • 相关阅读:
    在安装了Anaconda+Pycharm怎么导入OpenCV
    JS--ECMAScript
    JS--DOM
    JS-BOM
    浮动 高度塌陷
    CSS2--字体样式
    CSS2--文本样式
    css2--垂直对齐
    css2--背景
    HTML基础
  • 原文地址:https://www.cnblogs.com/thx666/p/8324250.html
Copyright © 2011-2022 走看看