Блог пользователя Kneee

Автор Kneee, история, 4 года назад, По-английски

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

  • Проголосовать: нравится
  • +10
  • Проголосовать: не нравится

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Kneee (previous revision, new revision, compare).

»
4 года назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

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

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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:

    //char arr[]={'m','e','o','w'};
    vector<char> arr = {'m','e','o','w'};

or experiment with:

    //char arr[]={'m','e','o','w'};
    char arr[]={'m','e','o','w',' '};
»
4 года назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

AC with GNU C++17 195871587

You need to add