bhavyammodi's blog

By bhavyammodi, history, 4 weeks ago, In English

Can anyone help me to understand that why my code gets runtime error in test #2

My submission : https://codeforces.com/contest/1509/submission/113226347

Thanks in advance!

 
 
 
 
  • Vote: I like it
  • +2
  • Vote: I do not like it

»
4 weeks ago, # |
  Vote: I like it +19 Vote: I do not like it

Have you tried the following:

  • Write a very naive and simple solution to the problem (maybe exponential complexity) (if you can, you may also just copy someone else's solution)
  • Write a program to generate thousands of small (!) test cases
  • Using those two, find a test case where your program gives the wrong answer
  • Use print statements or a debugger to see where exactly your program does the wrong thing.

98% of WAs and REs can be resolved this way. People here don't have the time to delve into every code posted here, it's much harder to debug somebody else's code and being able to debug your own code is a valuable skill. It is also a very routine process that can be learned much faster than problem solving and algorithms.

Credits: -is-this-fft-

»
4 weeks ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

You are submitting your code to a 32-bit compiler and string::npos returned by the "find" method happens to be a 32-bit unsigned value 0xFFFFFFFF.

You are also doing "#define int long long int" (a very ugly and error prone hack), which makes your temp1 variable 64-bit in the "int temp1 = n.find("T")" line.

Then your "if (temp1 != -1)" check doesn't work as expected because 0xFFFFFFFF != -1.

You can reproduce the problem by compiling your solution in 32-bit mode (using -m32 compile option for gcc) and running it with a testcase, which does not contain any "T" characters.

  • »
    »
    4 weeks ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Thank you very much! Will not use #define int long long int from now on.

    • »
      »
      »
      4 weeks ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      I think that people normally use "typedef long long ll;" because "ll" is even faster to type than "int". Redefining standard types to something else makes the code much harder to review and identify bugs.

      Regardless of that, a good recommendation is to preferably have the same compiler version/configuration on your local computer as used by the system that is judging your solutions. This way you are more likely to be able to reproduce and debug runtime errors if/when you encounter them.

      • »
        »
        »
        »
        3 weeks ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Actually, I am new to Codeforces and competitive coding and I have a habit of using int everywhere, I know that I need to change it but this seemed a very good shortcut, untill now! Thank you for guiding me. Can you help me, how should I install the "same compiler version/configuration". Currently, I am using Microsoft Visual Studio Code as my IDE.

        Thank you very much! :)