Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Findin lex smallest cyclic shift of a string
- * Algorithm :
- * Order : Linear O( n )
- */
- #include<string>
- using namespace std;
- /* min_ind contains the lowest index of s( 0 based ) which is the cyclic lex smallest */
- long MinCyclicShift( string s ){
- s += s;
- long n = s.length();
- long i = 0,min_ind = 0;
- while( i < n/2 ){
- min_ind = i;
- long j = i+1, k = i;
- while( j<n && s[k] <= s[j] ){
- if( s[k] < s[j] ) k = i;
- else k++;
- ++j;
- }
- while( i<=k ) i += j-k;
- }
- return min_ind;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement