gpt4 book ai didi

c++ - 为什么我的C++程序崩溃了。我是圆括号的实现问题

转载 作者:行者123 更新时间:2023-12-02 10:33:23 25 4
gpt4 key购买 nike

我正在对黑客等级实施平衡括号问题。 link
当我在hackerrank ide上运行我的程序时,它显示以下警告。

Solution.cpp: In function ‘std::__cxx11::string isBalanced(std::__cxx11::string)’:
Solution.cpp:51:17: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
if('{'!=s[i])
^~
Solution.cpp:54:21: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
bracket.pop();
^~~~~~~
Solution.cpp:25:16: error: control reaches end of non-void function [-Werror=return-type]
stack<char>bracket;
^~~~~~~
cc1plus: some warnings being treated as errors

当我在本地计算机上运行程序时,读取整数 (test case)后,程序崩溃。
为了简化代码,我添加了注释。
问题在 isBalanced()函数中显示。
我的代码:

#include <bits/stdc++.h>

using namespace std;

// return true if the passed character belongs to ar[], false otherwise
bool find(char ch,char ar[]){
for(int i=0;i<3;i++)
if(ar[i]==ch)
return true;

return false;
}


string isBalanced(string s) {
int size=s.length();
stack<char>bracket;
char open[3]={'{','[','('};

// if size of string is odd then expression is not balanced
if(size%2!=0)
return "NO";

for(int i=0;i<size;i++){
// if current bracket is opening bracket
// then push it into bracket stack
if(find(s[i],open)){
bracket.push(s[i]);
}
// if current bracket is closing bracket
// then match top element of bracket stack with current bracket
// if both are matched then pop an element from bracket stack
// if both are not matched return "NO"
else{
// if there is an element to match and current stack is empty then expression is not balanced
if(bracket.empty())
return "NO";

else
switch (bracket.top()) {
case '{':

if('{'!=s[i])
return "NO";

bracket.pop();
break;

case '(':
if('('!=s[i])
return "NO";

bracket.pop();
break;

case '[':
if('['!=s[i])
return "NO";

bracket.pop();
break;
}

bracket.pop();
}
return "YES";
}

}

int main()
{
int t;
cin >> t;
for (int t_itr = 0; t_itr < t; t_itr++) {
string s;
getline(cin, s);
string result = isBalanced(s);
cout << result << "\n";
}
return 0;
}

最佳答案

两件事情:

  • 不要混合使用getline和运算符>>。要么这样做:
  • int main()
    {
    int t;
    cin >> t;
    cin.ignore();
    for (int t_itr = 0; t_itr < t; t_itr++) {
    string s;
    getline(cin, s);
    string result = isBalanced(s);
    cout << result << "\n";
    }
    return 0;
    }

    或这样做:
    int main()
    {
    int t;
    cin >> t;
    for (int t_itr = 0; t_itr < t; t_itr++) {
    string s;
    cin >> s;
    string result = isBalanced(s);
    cout << result << "\n";
    }
    return 0;
    }

    视情况而定。
  • 您的逻辑有缺陷。这是isBalanced()的正确实现:
  • string isBalanced(string s) {
    int size=s.length();
    stack<char>bracket;
    char open[3]={'{','[','('};

    if(size%2!=0)
    return "NO";

    for(int i=0;i<size;i++){
    cout << i << endl;
    if(find(s[i],open)){
    bracket.push(s[i]);
    }
    else{
    if(bracket.empty())
    return "NO";
    else
    {
    switch (bracket.top()) {
    case '{':
    if('}'!=s[i])
    return "NO";
    break;

    case '(':
    if(')'!=s[i])
    return "NO";
    break;

    case '[':
    if(']'!=s[i])
    return "NO";
    break;
    }
    bracket.pop();
    }
    }
    }
    return bracket.empty() ? "YES" : "NO";
    }

    建议:请始终使用适当的缩进。

    关于c++ - 为什么我的C++程序崩溃了。我是圆括号的实现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61421254/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com