zoukankan      html  css  js  c++  java
  • 括号配对

    这是一段   WA代码...........
    #include <iostream>
    #include <stack>
    #include<string>
    using namespace std;
    void main()
    {
        stack<char> c;
        int T,len;
        cin >> T;
        getchar();
        string str;
        while (T--)
        {
            getline(cin, str);
            len=str.length();
            if (len % 2 != 0)
            {
                cout << "No" << endl;
                continue;
            }
            else if(len == 0)
            {
                cout << "Yes" << endl;
                continue;
            }
            if (str[0] == ')' || str[0] == ']')
            {
                cout << "No" << endl;
                continue;
            }
            for (int i = 0; i < len; i++)
            {
                if (str[i] == '(' || str[i] == '[')
                    c.push(str[i]);
                else
                {
                    if (str[i] == ')' && c.top()=='(' || str[i] == ']' && c.top() == '[')
                        c.pop();
                    else
                    {
                        cout << "No" << endl;
                        continue;
                    }
                }
                if (i == len - 1)
                {
                    if (c.size())
                        cout << "No" << endl;
                    else
                        cout << "Yes" << endl;
                }
            }
        }
    }
    AC代码:
    #include<iostream>
    #include<stdio.h>
    #include<string>
    using namespace std;
    string del(string str,int n)
    {
        if (str == "")
            return str;
        for (int i = n;i < str.size() - 1;i++)
        {
            if ((str[i] == '('&&str[i + 1] == ')')
                || (str[i] == '['&&str[i + 1] == ']'))
            {
                str.erase(i, 2);
                i = i > 0 ? i - 1 : i;
                return del(str, i);
            }
        }
        return str;
    }
    int main()
    {
        int T;
        cin >> T;
        getchar();
        while (T--)
        {
            string str;
            getline(cin, str);
            if (del(str,0) == "")
                cout << "Yes" << endl;
            else
                cout << "No" << endl;
        }
    }

  • 相关阅读:
    centos 7 开放端口
    删除mysql 表中重复的数据
    约瑟夫问题
    Technocup 2020 Elimination Round 3题解
    DISCO Presents Discovery Channel Code Contest 2020 Qual题解
    Comet OJ
    Berlekamp-Massey算法
    CH定理与线性递推
    2020集训队作业板刷记录(一)
    模拟费用流
  • 原文地址:https://www.cnblogs.com/edych/p/7202534.html
Copyright © 2011-2022 走看看