Can someone please explain the suffix array approach to this problem: http://www.spoj.com/problems/MINMOVE/
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
Can someone please explain the suffix array approach to this problem: http://www.spoj.com/problems/MINMOVE/
Name |
---|
This corresponds to the lexicographically smallest string and the rotations needed will just be the index value.
I had a similar approach..but it fails for string like aaa..your answer would be 2..but answer is 0.
Yes, you are correct. The answer would be 2 if we use the normal suffix array code. But, we can tweak the code to work in this case. The problem is strings that are completely prefix of another string will be taken to have a lower sorted index. Depending on how we have written the suffix array, it can be changed. For example, take the suffix array code from this link : Ideone (Code taken from codechef's suffix array tutorial page)
When we look at line number 37, we have a condition check and a "-1" being inserted. This is to make sure, when sorting it comes before strings with the same prefix. So, if we replace this with a high number like "1e9" then while sorting, it will come after the strings of higher length with the same string as its prefix.
By doing this, the same approach as stated above will work and now we will get the answer as 0 and not 2, according to the my understanding. Please let me know if I have missed out anything.
Thanks for the great explanation!
Note that, if you append the string to itself, every substring of length N will be a rotation.
Let's call the input string S and the new string T. You must find the lexicographically smallest substring of T with length N and break ties using indexes (the substring at position i represents i rotations).
To do this, you can construct the suffix array of T and look for the first suffix located before position N (remember that length(T) = 2N).
This will work for tests without ties, but what about strings like aaa or abcabc?
Let's say you found the suffix at position p (p ≤ N / 2). Now you must consider all suffixes that have the suffix located at p as a preffix, and the answer should be the leftmost suffix you find. This can be easily done using the Longest Common Preffix.
Hope this helps!
But what is the need to break such ties? In the sorting potion of the suffix array creation algorithm, we can use a stable sort algorithm and that would ensure that two suffixes that are essentially the same will be sorted in the order of whichever has a lower starting position, thus emitting a minimum rotational move. Since radix sort is stable, no other rbreaking of ties would be required I guess.
If I am missing something or overlooking some case, then please do point it out.
cc: @bernett