insurgentes's blog

By insurgentes, history, 3 years ago, In English

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://codeforces.net/contest/1538/problem/E

RTE (C++17): https://codeforces.net/contest/1538/submission/123976702

Accepted (C++17 64bit): https://codeforces.net/contest/1538/submission/123976695

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

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I guess it's the 2nd line of hahaCount function.

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

    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://codeforces.net/contest/1538/submission/123980050

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

      In RHS the return type of std::string::size is unsigned long. [reference]

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

      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

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

        thanks for the detailed breakdown, this was super helpful!