Schnider's blog

By Schnider, history, 4 years ago, In English

So, I tried solving the problem in question, here is a link to it 1385D - a-Good String, but I faced a TLE. In the first submission 87154198 I tried to loop over the string every time to count the number of characters, and that is probably a stupid thing to do and takes a long time to execute even though I looks somewhat similar to the tutorial code. But I tried it again, 87258034 and basically I prepared an array beforehand that contains the number of characters up to that point, and that made the backtracking method O(1) in every iteration. I know that I'm doing a (26 * 131072) iterations per test case, but that's it. I'm thinking that the posted solution doesn't do a lot better because it's using the count() method, and cplusplus reference website says that count is O(n). Does anyone have any ideas why the posted solution is fine while mine got a TLE? Any help is appreciated

UPD: I got it accepted with almost the same code, but with a few simple modifications. To be perfectly honest, I have no idea why the previous code gave a TLE while this one worked, but if you're interested, here's the code 87480391

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it