Represent the minimum and maximum string represents

Given an annular string s, seeking string t, such that t s is the length of all strings in the same minimum and maximum string string lexicographically

E.g:

Is the minimum string abcdaad aadabcd, maximum string is dabcdaa

'5612358' is the minimum string '1235856', the maximum string is '8561235'

 

// Returns a string representation of the minimum 
function getMin (S) { 
    the let I, J, K, L; 
    const = N s.length; 
    S = S +; 
    for (I = 0, J =. 1; J <N;) { 
        for (K = 0; K <N && S [K + I] === S [J + K]; ++ K); 
        IF (K> = N) BREAK; 
        IF (S [K + I] <S [J K +]) 
            J = K + +. 1; 
        the else { 
            L = I + K; 
            I = J; 
            J = Math.max (L, J) + 1'd; 
        } 
    } 

    return s.substr (I, N) 
} 

// returns represents the maximum string 
function getMax (S) { 
    the let I, J, K, L; 
    const = N s.length; 
    S = S +; 
    for (I = 0, J =. 1; J <N;) { 
        for (k = 0; k < N && s[i + k] === s[j + k]; k++) ;
        IF ( k> = N) break;
        if (s[i + k] > s[j + k])
            j += k + 1;
        else {
            l = i + k;
            i = j;
            j = Math.max(l, j) + 1;
        }
    }

    return s.substr(i, N)
}

  

  

Guess you like

Origin www.cnblogs.com/caoke/p/10948923.html
Recommended