U314765括号匹配求助(附代码)
查看原帖
U314765括号匹配求助(附代码)
1040131
WangZhaoyun0215楼主2023/9/28 15:20

P5660代码

#include <bits/stdc++.h>

using namespace std;

int n,s[100001],top;
char str[100011];

int main()
{
    cin >> n >> str + 1;
    for(int i = 1;i <= n;i++)
    {
        if(str[i] == '(')
            s[++top] = 0;
        else
        {
            if(str[i] == '[')
                s[++top] = 1;
            else
            {
                if(!top)
                {
                    printf("NO\n");
                    return 0;
                }
                if(str[i] == ')')
                {
                    if(!s[top])
                        --top;
                    else
                    {
                        printf("NO\n");
                        return 0;
                    }
                }
                else
                {
                    if(s[top])
                        --top;
                    else
                    {
                        printf("NO\n");
                        return 0;
                    }
                }
            }
        }
    }
    if(top)
        printf("NO\n");
    else
        printf("YES\n");
    return 0;
}

2023/9/28 15:20
加载中...