• 1355:字符串匹配问题(strs)


    【题目描述】

    字符串中只含有括号 (),[],<>,{},判断输入的字符串中括号是否匹配。如果括号有互相包含的形式,从内到外必须是<>,(),[],{},例如。输入: [()] 输出:YES,而输入([]),([)]都应该输出NO

    【输入】

    第一行为一个整数nn,表示以下有多少个由括好组成的字符串。接下来的nn行,每行都是一个由括号组成的长度不超过255255的字符串。

    【输出】

    在输出文件中有nn行,每行都是YESNO

    【输入样例】

    5
    {}{}<><>()()[][]
    {{}}{{}}<<>><<>>(())(())[[]][[]]
    {{}}{{}}<<>><<>>(())(())[[]][[]]
    {<>}{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]
    ><}{{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]

    【输出样例】

    YES
    YES
    YES
    YES
    NO

    #include <iostream>
    using namespace std;
    
    const int N = 255;
    char a[N] = {0};
    
    int main()
    {
        int n;
        cin >> n;
        char ch = cin.get();//return char
        //cout<<n<<":"<<ch<<endl;
        for (int i = 0; i < n; i++) {
            int jian, xiao, zhong, da, top, cnt;
            for (ch = jian = xiao = zhong = da = top = cnt = 0;
                 ch != '@' && cnt < N; cnt++) {//从内到外必须是<>,(),[],{}
                //cin>>ch;//none space
                ch = cin.get();
                //cout<<ch;
                switch (ch) {
                    case '<':
                        a[top++] = ch;
                        jian++;
                        break;
                    case '>':
                        jian--; //尖括号
                        if (a[--top] != '<') {
                            //cout<<">:"<<a[top]<<endl;
                            cin >> a; //remove a line
                            ch = cin.get();//return char
                            ch = '@';
                        }
                        break;
                    case '(':
                        a[top++] = ch;
                        xiao++;
                        break;
                    case ')':
                        xiao--; //小括号
                        if (a[--top] != '(' || jian != 0) {
                            //cout<<"):"<<a[top]<<endl;
                            cin >> a; //remove a line
                            ch = cin.get();//return char
                            ch = '@';
                        }
                        break;
                    case '[':
                        a[top++] = ch;
                        zhong++;
                        break;
                    case ']':
                        zhong--; //中括号
                        if (a[--top] != '[' || jian != 0 || xiao != 0) {
                            //cout<<"]:"<<a[top]<<endl;
                            cin >> a; //remove a line
                            ch = cin.get();//return char
                            ch = '@';
                        }
                        break;
                    case '{':
                        a[top++] = ch;
                        da++;
                        break;
                    case '}':
                        da--; //大括号
                        if (a[--top] != '{' || jian != 0 || xiao != 0 || zhong != 0) {
                            //cout<<"}:"<<a[top]<<endl;
                            cin >> a; //remove a line
                            ch = cin.get();//return char
                            ch = '@';
                        }
                        break;
                    case '@':
                    default:
                        //cout<<"@:"<<ch<<endl;
                        ch = '@';
                        break;
                }
            }
            //cout<<i<<":"<<ch<<endl;
            if (jian == 0 && xiao == 0 && zhong == 0 && da == 0) {
                cout << "YES" << endl;
            } else {
                cout << "NO" << endl;
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    git切换分支报错解决
    网上看到的一个布局神器
    js获取滚动条,文档,浏览器高度
    使用iView-cli创建一个pc端网页(1)
    ajax详解与用途
    JavaScript中for/in和for的区别
    排序
    Java中getResourceAsStream的用法
    java程序打包成exe的总结
    使用7zip把jre集成到绿色运行程序内
  • 原文地址:https://www.cnblogs.com/gaojs/p/15584375.html
Copyright © 2020-2023  润新知