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

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

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.com/contest/1538/problem/E

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

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

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

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

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

Spoiler
  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.com/contest/1538/submission/123980050

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится +8 Проголосовать: не нравится

      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