Here is the problem I solved: http://codeforces.me/problemset/problem/765/A). I submitted a code (My code: http://pastebin.com/8FckK356 ). It gives WA on test 11 (test 11: http://pastebin.com/g7MC6YJf ). But in my computer I paste the test and it outputs the correct result. The logic in my code is correct. So why does the CodeForces judge output a different result for my code?
airName length is 3, but you read airName[j] for j=5..7
5, 6, 7 are three. I chose a different compiler and it worked. I want to know Why GNU G++11 5.1.0 did not work, but worked on GNU G++14 6.2.0
It's undefined behavior. You can only access airName[j] for j=0..2
thanks
It is an UB and you are stupid. Almost always if solution is ok on local machine and fails in judge the problem is out-of-bound index.
Submit your code with G++ 6.2 c++14 compiler )))))). 24671484
Thanks for the response. Could you please explain why it works on G++ 6.2 c++14 compiler, but not on GNU G++11 5.1.0?
My coatch told as: Very bad bug is a bug, where works correct.
I don't know why this wrong solution passes all tests with G++ 6.2 C++14.
I found, G++ 6.2 used continues content for
string
class,and do not allocate memory for less than 15 symbols (may less 23, I cannot remember).
so, access to non-exist element is safe, if index is tiny.
Thanks for pointing out the bug.
If you want AC this code for any compiler, it's very simple:
Declare
airName
string as global !!.Why does it work? We want to compare nextS[5..7] to airName[0..2], so why would airName[5..7]==airName[0..2] now?
Let me explain.
The algorithm which count number of
nexts[5..7] == airName[0..2]
and if this count is even answer is 'home' otherwise 'contest' is WRONG!
But this WRONG algorithm implemented also WRONG way, and resulting implementation is CORRECT !!!
CORRECT algorithm : if n is even number answer='home' otherwise 'contest'.
Now, if
airName
is global, all its content fills with zero by default.Note that, GCC c++11 new ABI, and VS++ compilers wouldn't allocate memory from heap for string, if size is small ( ~ 15-23 ), instead of uses its stack memory.