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

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

my submission: https://codeforces.me/contest/126/submission/190247628 //

as far as I can see, My solution is O(n²) when there is no solution and it got AC. and the string length is ~~ 1e6

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

»
4 года назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

This problem is very old (Beta round) so test cases are likely to be weak.

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

It's actually not $$$n^2$$$. The hashes are done in $$$O(1)$$$ so comparing each suffix with the prefix is $$$n \cdot O(1)$$$. Suppose $$$A$$$ is the shortest substring of $$$S$$$ such that $$$A$$$ is both a suffix and a prefix. Then $$$S = ATA$$$. Suppose $$$AB$$$ is the second shortest substring such that $$$AB$$$ is both a suffix and a prefix. Then $$$S = ABT^\prime AB$$$. This implies that $$$A$$$ was also an infix of $$$S$$$. So when we actually ran the algorithm, we should've stopped at just $$$A$$$.

What all this implies is that there can be at most one candidate prefix/suffix. If there is more than one, we know that the first candidate also appears somewhere inside of $$$S$$$ and we'd just print it. This makes the algorithm $$$O(n)$$$.

(Edit: simplified argument slightly)