Can someone please explain the suffix array approach to this problem: http://www.spoj.com/problems/MINMOVE/
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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