Minimal cyclic shift algorithm explanation

后端 未结 2 548
青春惊慌失措
青春惊慌失措 2021-02-15 17:39

I have recently came up against this code lacking any comment. It finds minimal cyclic shift of word (this code specifically returns its index in string) and its called Duval al

2条回答
  •  梦毁少年i
    2021-02-15 18:12

    It may be the same as this algorithm, whose explanation can be found here:

    int ComputeMaxSufPos(string w)
    {
        int i = 0, n = w.Length;
        for (int j = 1; j < n; ++j)
        {
            int c, k = 0;
            while ((c = w[(i + k) % n].CompareTo(w[(j + k) % n])) == 0 && k != n)
            { k++; }
            j += c > 0 ? k / (j - i) * (j - i) : k;
            i = c > 0 ? j : i;
        }
        return i;
    }
    

提交回复
热议问题