Hi everyone,
Can anyone explain why this code gets Wrong Answer on Test Case 2, Although the same code just deletes the map (didn't use it), it gets Accepted
If you submitted the same code that got wrong with c++20, it will give AC but c++17 gives the wrong answer
Code with map c++17: 195842451 Wrong Answer on Test Case 2
Code with map c++20: 195842466 Accepted
Code without map c++17: 195842512 Accepted
Thanks
Auto comment: topic has been updated by Kneee (previous revision, new revision, compare).
Probably due to undefined behavior which is present in your code. You should replace while(s[i] == arr[cur]) with while(i<s.length() && s[i] == arr[cur]). Also, your cur variable can go past 3, so you are accessing index 4 of {'m', 'e', 'o', 'w'} which is undefined. Fixing these undefined behavior lets the code pass in C++ 17. Why it passes when you delete the map? We can't ever know for sure, undefined behavior doesn't guarantee the code will be wrong, it just means it might not work as expected sometimes.
Fixed code with map included that passes in C++ 17: 195846468
Thanks for your effort! ❤️
can you help me with my code too for the same question. 195883758 this code gives wrong ans on 2nd test, it returns a YES when it should return a NO. thanks
Your second if statement seemed dubious to me. The || s[n-1] == 'w' isn't evaluated in the order you think it is. I recommend using parentheses when using && and || in the same if statement. Fixed solution: 195885580
Array out of bounds:
Line 45: Char 18: runtime error: index 4 out of bounds for type 'char [4]' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:50:18
Try changing to use this:
or experiment with:
Thanks for your effort! ❤️
What makes me confused when I deleted the map it gets accepted, so weird
char arr[]={'m','e','o','w',' '};
How can this help? Error is due to the fact that index is becoming >= length of array, independent of what is length
I assume the original logic is only off-by-one and that the last dummy character read does not affect the logic. Anyways, I just wanted OP to experiment with the code change, not to use the code directly.
AC with GNU C++17 195871587