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
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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
Название |
---|
This problem is very old (Beta round) so test cases are likely to be weak.
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)
But in the code I m not stopping at the shortest, the opposite, I m searching for the longest ones , starting from the prefix at string.length()-2
My bad for not noticing but the idea is the same. Here we could call the longest suffix/prefix $$$AB$$$ and the second longest $$$A$$$ and the rest of the argument is identical.