问题 C: 数据结构基础3-字符串匹配问题

题目描述

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

输入

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

输出

在输出文件中有N行,每行都是YES或NO。

这个题目一开始我有三个测试点没有通过,后来检查了一下发现是因为我宏定义了我的栈,导致如果我的栈如果前面有数据就会影响后面的输出结果,实际上这个栈应该定义在for循环里面的,这样就ac了,下面是我的代码

样例输入 Copy
5
{}{}<><>()()[][]
{{}}{{}}<<>><<>>(())(())[[]][[]]
{{}}{{}}<<>><<>>(())(())[[]][[]]
{<>}{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]
><}{{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]
样例输出 Copy
YES
YES
YES
YES
NO

#include<iostream>
#include<stack>
using namespace std;

int priority(char ch)
{
    int res = 0;
    switch(ch)
    {
        case '<':
            res = 1;
            break;
        case '(':
            res = 2;
            break;
        case '[':
            res = 3;
            break;
        case '{':
            res = 4;
            break;
    }
    return res;
}
int main()
{
    int n;
    string s;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> s;
        stack<char> shi;
        int flag = 0;
        for (char ch : s)
        {
            if (ch == '[' || ch == '{' || ch == '(' || ch == '<')
            {
                if (shi.empty() || priority(ch) <= priority(shi.top()))
                {
                    shi.push(ch);
                    continue;
                }
                else if (priority(ch) > priority(shi.top()))
                {
                    flag = 0;
                    break;
                }
            }
            else if (ch == ']')
            {
                if (shi.empty() || shi.top() != '[')
                {
                    flag = 1;
                }
                else
                {
                    shi.pop();
                    continue;
                }
            }
            else if (ch == ')')
            {
                if (shi.empty() || shi.top() != '(')
                {
                    flag = 1;
                }
                else
                {
                    shi.pop();
                    continue;
                }
            }
            else if (ch == '}')
            {
                if (shi.empty() || shi.top() != '{')
                {
                    flag = 1;
                }
                else
                {
                    shi.pop();
                    continue;
                }
            }
            else if (ch == '>')
            {
                if (shi.empty() || shi.top() != '<')
                {
                    flag = 1;
                }
                else
                {
                    shi.pop();
                    continue;
                }
            }
        }
        if (!shi.empty())
        {
            flag = 1;
        }
        if (flag == 1)
        {
            cout << "NO" << endl;
        }
        else if (flag == 0)
        {
            cout << "YES" << endl;
        }
    }
    return 0;
}

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值