0
votes

I was writing a simple c++ program

#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;
}

and input it with *

5
2
))
12
gl))hf))))))
9
gege)))))
14
)aa))b))))))))
1
)*

but it is showing output i am unable to understand please help

/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 = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]: Assertion '__pos <= size()' failed.

2
You have an out of range indexing error, did you try to debug your program?Quimby
but it is working on my friend's laptop same code same inputSuryakant
Yea, that is the definition of undefined behaviour you have in your code. Start your favourite debugger and see what and why is causing that.Quimby
yeah i got my error but I am still unable to find how it worked on my friend's computer? and it also accepted on codeforces? how it worked plzz explain is it due to different compiler version like mine is MinGW 32 and he has Mingw64?Suryakant
I am not sure what is unclear. The behaviour is undefined, it might do anything, that includes working. In particular, it looks like your compiler has extra checks in place. Other compilers might just accept s[-1] with the mindset "if it crashes, not our fault".Quimby

2 Answers

1
votes

This is wrong

while (s[i] == ')' && i > -1)

it should be

while (i > -1 && s[i] == ')')

If i equals -1 then in the first version s[i] gives you the error you can see. But in the second version i > -1 is evaluated first and because it's false s[i] is not evaluated and so does not cause an error.

There's an important lesson here as well. This program worked on your friends machine but not every program with an error fails. It might even seem to run correctly.

0
votes

The assertion __pos <= size() means you are accessing the string s using the [] operator past the end. Run the program in your debugger and either set a conditional break point on the index > size(), or just inspect every access.

  1. Both s and n are read from cin (opposed to n = s.size()), if n > s.size() + 2 should trigger it.
  2. n is type (signed) long long, and operator[] expects a size_t (unsigned long) so n <= 0 would probably trigger this too as you do n - 1 on access.
  3. In your while loop you s[i] == ')' && i > -1 with i = -1 would first try to access s[-1] before checking i > -1 as the the && operator as an associativity of left-to-right.