Algorithm: minimum representation of a string

Source: Internet
Author: User

The minimum representation of a string is that the last character of a string can be put in the first place, and so on. There are N types of deformation, where N is the length of the string.

For example:

S = "00ab"

Deformation with (Omitted quotation marks) b00a ab00 0ab0

Four Types

Find the smallest Lexicographic Order, and use this algorithm.

Define three pointers, I, J, K

Initial I = 0; j = 1; k = 0

First, if s [I] <s [J], it is obvious that J ++

If s [I]> S [J], it is obvious that I = J ++

If s [I] = s [J.

In this case, all the characters between I and j must be greater than or equal to s [I ].

The other K is 0, and the first s [I + k] is searched cyclically. = S [J + k] Location

If s [I + k] <s [J + k] Then J + = k + 1

Why?

First s [I] to s [I + k-1] must be greater than or equal to s [I], because if one of the numbers is less than s [I], so this number must exist in S [J] To s [J + k-1], and because there must be one in the back, so if s [J] first met, then it will not continue to the K position, so there must be no character smaller than s [I.

Therefore, starting from any character in the sequence as the starting point will not be smaller than the current one. Therefore, it may be the minimum value only when the character after the selected sequence starts.

So J + = k + 1

If a number in the sequence is equal to the number of S [I], it must start before or after this position, so you do not need to start from this position.

Here I and j are equivalent. I have the same results before J, So I and j have the same processing. I will not explain it carefully, directly paste the Code:

In addition, if I = J, then let J ++ return to the original state.

In the end, it must be a small one that won't be moved, but the big one will keep moving backward. Therefore, you only need to output the smallest one of I and J.

int getmin(char *s){    int n=strlen(s);    int i=0,j=1,k=0,t;    while(i<n && j<n && k<n){        t=s[(i+k)%n]-s[(j+k)%n];        if (!t) k++;        else{            if (t>0) i+=k+1;             else j+=k+1;            if (i==j) j++;            k=0;        }    }    return i<j?i:j;}
View code

Algorithm: minimum representation of a string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.