其他分享
首页 > 其他分享> > 1355:字符串匹配问题(strs)

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;
}

  

标签:case,ch,strs,top,cin,1355,char,--,字符串
来源: https://www.cnblogs.com/gaojs/p/15584375.html