Minimum Lexicographic Rotation Using Suffix Array

后端 未结 3 1929
你的背包
你的背包 2021-01-06 10:08
    Consider a string of length n (1 <= n <= 100000). 
    Determine its minimum lexicographic rotation. 
    For example, the rotations of the string “alabala         


        
相关标签:
3条回答
  • 2021-01-06 10:39

    If you are using O(n log n) algorithm (sort by first letter, then by first two letters, then by first four, ...), you can make a little bit modified suffix array.

    Don't sort suffixes of string but it's cyclic rotations. It should be really small modification in the algorithm. A then you will get desired result directly.

    If you still want to use your method, then just take first index which is between 0 and N.

    0 讨论(0)
  • 2021-01-06 10:52

    It seems that you should take first suffix in SA, which index is between 0 and length(S) - 1.

    Some explanation: all rotations of S are in the beginning of S' suffixes from positions between 0 and length(S) - 1. Suffix array keeps suffixes in lexicographical order, so you just need to pick the first one which begins from rotation of S.

    0 讨论(0)
  • 2021-01-06 11:00

    Thanks all .Both the answer by vkorchagin and usamec is correct for most Test Cases ,but they won't work for the Following Test Case(S="baabaa")

    S=baabaa; S'=baabaabaabaa;

    Suffix| Suffix  |  Suffixes
    Index | Length  |
    
    11      1       a
    10      2       aa
    7       5       aabaa
    4       8       aabaabaa
    1       11      aabaabaabaa
    8       4       abaa
    5       7       abaabaa
    2       10      abaabaabaa
    9       3       baa
    6       6       baabaa
    3       9       baabaabaa
    0       12      baabaabaabaa
    

    Taking the First Suffix whose Index is between 0 to S.length()-1 does not work for the above test case.If I does so ,then the result is 4 ,but the correct answer is 1.

    So I modified the answer ,a bit .

    This is what i did or added/modified an extra condition to the above answers ::

    (1)I took the First Suffix whose Index is between 0 to S.length()-1 .

    Lets say its index is :=ExpectedIdx.

    In the above Example ExpectedIdx=4.

    (2).Now the ExpectedIdx may or may not be the answer. The reason is the Next suffix in Suffix Array may produce the same answer.

    Example ::

    Taking the suffix whose starting Index is 4 (ExpectedIdx),aabaabaa.,we get aabaab as Minimum Lexograhic rotated String.

    Taking the Next suffix , aabaabaabaa.

    We also get aabaab as Minimum Lexograhic rotated String.

    But the former one requires the shift of 4 while the latter one requires the shift of 1 .So correct answer is 1 ,not 4.

    So i used the concept of Longest Common Prefix (LCP) to check the similarities and Finally got accepted.http://livearchive.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=756

    Edit:: This is the Pseudocode -

    int ExpectedIdx,ExpectedSuffixNumber,ExpectedSuffixLength;
    for(int i=0;i<strlen(str);++i)//str = Length of S'
    {
        suffixsize=strlen(str)-SA[i];
        if(suffixsize>(Len/2))//Len/2:=Size of S
        {
            ExpectedIdx=SA[i];
            ExpectedSuffixNumber=i;
            ExpectedSuffixLength=suffixsize;
            break;
        }
    }
    //Now this ExpectediDx may or may not be the correct answer.
    
    int finalans=ExpectedIdx;//Lets assume initially that ExpectedIdx is a correct/final answer.
    for(int i=(ExpectedSuffixNumber+1);i<Len;++i)//Check the Next Suffix 
    {
        if(LCP[i]>Len/2)//LCP[i]=Lingest common prefix of adjacent prefixes in a suffix Array.
        {
            if(SA[i]>finalans)
            {
                finalans=SA[i];
            }
        }
        else
            break;
    }
    
    0 讨论(0)
提交回复
热议问题