Codeforces and Polygon may be unavailable from December 6, 19:00 (UTC) to December 6, 21:00 (UTC) due to technical maintenance. ×

ezdp's blog

By ezdp, history, 5 years ago, In English

[Problem]: 1200E - Compress Words [Solution]: 74203908

I'm getting a TLE on test 3. I'm using hashing to answer queries "Is string A equal to string B?" in O(1) time with O(N) preprocessing time. I'm doing N queries at worst so my time complexity should be O(N + N) = O(N).

As I'm just learning hashing I was using this solution(58603158) to check if I'm doing the right things. The only difference in the two solution should be(I might have overlooked something, if that's the case I'm sorry) that I'm using two-dimensional arrays to save the two hashes per prefix, where the other solution is using vectors. The other difference is that instead of using modular multiplicative inverse to "divide" the hash of the substring I'm querying for and "drag" it such that there are 0 empty spaces before it, I'm multiplying so that I "push" the substring to the end so that there are MAXN(1e6 + 5 in my code) empty spaces.

Let's say in string "abcdefg", I'm querying for substring from index 4 to 6 (0-indexex): 1) I will subtract the hash of prefix "abcd" from prefix "abcdefg". The result will be "____efg"(_ is a empty space, 0 value); 2) Instead of "dragging" the substring I'm querying for so that I get the hash for "efg", I multiply it so that I get MAXN(1e6 + 5 in my code) empty spaces "_" and "efg" at the end.

I hope that made my idea more clear.

Thanks in advance for the help!

UPD1: I and a friend of mine tried some optimizations and found that the f() function was not working properly(it returned the same values for 'X' and 'x') but I still get TLE on test 3.

  • Vote: I like it
  • -5
  • Vote: I do not like it

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

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

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

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

»
5 years ago, # |
Rev. 3   Vote: I like it +4 Vote: I do not like it

When putting the string t into a function, you make a copy of it every time and you also spend a lot of time returning it. Try passing it by reference and making the function void instead(or simply don't put it as an argument at all, it's a global variable). a = a + b[i] is too slow. You can replace it with a += b[i]. Also, you're making a Hash struct $$$n$$$ times, and each time you create an array of size 2*MAXN.

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

    Thanks a lot! I got an AC now.

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

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