【发布时间】:2021-04-02 20:31:50
【问题描述】:
我正在编写一个简单的 c++ 程序
#include <bits/stdc++.h>
#define ll long long
#define ul unsigned long long
#define ld long double
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define repi(i, a, b) for (int i = (a); i > (b); i--)
#define all(x) x.begin(), x.end()
#define ks(x) (cout << #x << ":" << (x) << '\n')
#define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define gcd _gcd
using namespace std;
const ll mod = 1000000007;
int main()
{
fastio;
ll tc = 1;
cin >> tc;
for (ll t = 0; t < tc; t++)
{
ll n;
cin >> n;
string s;
cin >> s;
ll cnt = 0;
ll i = n - 1;
if (s[n - 1] == ')')
{
i--;
cnt++;
while (s[i] == ')' && i > -1)
{
i--;
cnt++;
}
}
if (cnt > n / 2)
cout << "YES\n";
else
{
cout << "NO\n";
}
}
return 0;
}
并用输入 *
5
2
))
12
gl))hf))))))
9
gege)))))
14
)aa))b))))))))
1
)*
但它显示的输出我无法理解,请帮助
/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libstdc++-v3/include/bits/basic_string.h:1067: std::__cxx11::basic_string<_chart _traits _alloc>::reference std::__cxx11::basic_string<_chart _traits _alloc>::operator[](std::__cxx11::basic_string<_chart _traits _alloc>::size_type) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator; std::__cxx11::basic_string<_chart _traits _alloc>::reference = 字符&; std::__cxx11::basic_string<_chart _traits _alloc>::size_type = unsigned int]: 断言 '__pos
【问题讨论】:
-
您有一个超出范围的索引错误,您是否尝试调试您的程序?
-
但它在我朋友的笔记本电脑上运行相同的代码相同的输入
-
是的,这就是您的代码中未定义行为的定义。启动您最喜欢的调试器,看看是什么原因造成的。
-
是的,我收到了错误,但我仍然无法找到它在我朋友的计算机上的工作原理?它也被codeforces接受?它是如何工作的请解释一下是不是因为不同的编译器版本,比如我的是 MinGW 32 而他有 Mingw64?
-
我不确定有什么不清楚的地方。行为是未定义的,它可以做任何事情,包括工作。特别是,您的编译器似乎有额外的检查。其他编译器可能只是接受
s[-1]的心态“如果它崩溃了,不是我们的错”。