I'm getting a runtime error when i submit my code using C++ 17, but the same code is accepted for C++ 17 64 bit. I usually make all my submissions with C++17, so I'm interested in figuring out which line is causing runtime error.
Problem: https://mirror.codeforces.com/contest/1538/problem/E
RTE (C++17): https://mirror.codeforces.com/contest/1538/submission/123976702
Accepted (C++17 64bit): https://mirror.codeforces.com/contest/1538/submission/123976695
I guess it's the 2nd line of hahaCount function.
assignment of type unsigned long to type long long.
Thanks you're absolutely right[1]! Unfortunately I still don't understand what's wrong with that line :(
Sorry if this is a very basic question, but my understanding is that LHS and RHS are both signed here long long maxL = s.size() — 4;
[1] Answer accepted after i got rid of that line: https://mirror.codeforces.com/contest/1538/submission/123980050
In RHS the return type of std::string::size is unsigned long. [reference]
oof, my bad. thanks a lot!
Ok so what i think i figured out what is happening: string::size() returns a number of type size_t, which is unsigned long long on 64 bit systems and unsigned long (i.e unsinged int) on 32 bits.
If s.size() is less than 4, s.size()-4 will give you a unsigned 32 bit integer and when converting it to a long long, it will just copy the value instead of noticing its a negative number. That way, you can actually call substr() to a index that doesn't exist in your string and give you a runtime error
thanks for the detailed breakdown, this was super helpful!