Codeforces и Polygon могут быть недоступны в период с 23 мая, 7:00 (МСК) по 23 мая, 11:00 (МСК) в связи с проведением технических работ. ×

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

Автор exh3, история, 7 лет назад, перевод, По-русски

Всем привет!

В ходе решения 4C - Система регистрации возникла интересная ситуация. Моё решение: 24184665. Суть в том, что мой компилятор и компилятор Codeforces выдает разные ответы. Компилятор — MS VS C++ 2010. Пример:

Входные данные: test data

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

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

Since the standard doesn't specify the implementation of hash functions, the output is environment-dependent. Actually std::hash can even produce different hash values for the same input in different executions.(Since C++14)

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

    But during one execution it should return the same hash codes for the same values. It's the main principle of hash alghorithms. The question is in generation different hash-codes for the same objects during one execution.

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

      The issue isn't "different hash-codes for the same objects during one execution" which is a library bug. It's the same hash value for different objects, in other words, hash collision. The string "idcvexvhgtyyvplfr" and "idcvexvhgtyyvplfrl" generates the same hash value for the compiler you selected.

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

        MS VS C++ 2010 hash function only looks at a part of the string according to this StackOverflow post. A bit odd, but not a bug.

        • »
          »
          »
          »
          »
          7 лет назад, # ^ |
          Rev. 2   Проголосовать: нравится +5 Проголосовать: не нравится

          Yes. Maybe my previous comment isn't clear. I meant generating different hash for an object in an execution is a bug. Hash collision, although only looks part of a string is weird, isn't a bug.

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

            Oh, I understood. Thank you, guys. The solution is very simple — write your own hash function.

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

              Writing your own hash function is not going to prevent you from getting collisions. Is there a reason why you're not using a hash table?

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

                There isn't. Simply, hash function was the first solution, which I found as optimal. But practice has shown that it isn't the best one. Thank you for a helpfull tips.