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

Автор skpro19, история, 7 лет назад, По-английски

I am getting a WA on testCase 6.

Question — Div 2C

My Submission

Can someone please tell me what's wrong with the code?

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

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

Ok.

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

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

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

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

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

I changed s[j] to c(to fix the logic at your code) and get WA at test 16 instead. 25885244

I think there is hash collision which cause this to still fail. So either use a better hash function or don't hash! :)

UPD: The problem do lies in the hashing. I simply change the hash function and now the answer is wrong on another line

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

    But I just used a polynomial hash function as given in the tutorial for the question

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

      I have no idea then. But looking at most people's code, they have better hash function than a polynomial hash.

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

Your function returns this same hash for strings "af" and "ba"... Seems like it isn't very good... Try changing 5 for something bigger, it should help (bigger than 26). I haven't look for other bugs, so I am not sure that it will help.

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

You can get rid of hashing by replacing set<ll> with set<string> and still get WA 6: 25894661. It means that your solution has some other problems. I'd recommend finding them first, getting TL, and add hashing only afterawards.

Also, as far as I can see, your hashing function is just fine.

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

    I fix the logic as pointed out above, and got TLE on test 28. 25929172.

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

      Great! Have you tried adding hashing back?

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

        I did that above, and it got WA on test 16. 25885244

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

          Ok, I believe that there is a chance of collision when using small modulo (like 109 + 7). Birthday paradox tells us that if there are approx. random hashes, then chances of collision are greater than 50%. I'd suggest using bigger module as long as you use long long — like 1018 + 9 (it's prime, which is important).

          Afterwards you'll find out that your code is in fact quadratic, because calculation of a single hash takes linear time in your code (can be done more efficient by recalculating hash when changing the character). Try optimizing that. And afterwards you may encounter that endl makes your program extremely slow — replace it with "\n" if it's the case.

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

            yup, i agree with you. I am not the poster of this problem btw. Just trying to help out here.

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

1 1
ab
ab
should be no, but your code says yes.