UVa 1584
Title: Given an n-letter ring string, you can start from any position by reading n letters clockwise, the output of which is the smallest dictionary order results
The idea of solving a problem: first using modulo operation to achieve a given ring string and two initial position, compare the two dictionary order size function,
Then use a layer of loops, make n comparison, save the minimum dictionary sequence of the first letter position, and then use the output of the modulo operation can be
/*UVa 1584 Circular Sequence---water problem*/#include<cstdio>#include<cstring>//The string s is annular, p Q is the starting position length is n, the p is judged to be less than QintLessConst Char* S,intPintq) { intLen =strlen (s); for(inti =0; i < Len; ++i) { if(s[(p + i)% len] > s[(q + i)%Len]) { return-1;//p > Q } Else if(s[(p + i)% len] < s[(q + i)%Len]) { return 1;//Q < Q } } return 0;//Equal}intMain () {Chars[ the]; intT; scanf ("%d", &t); while(t--) {scanf ("%s", s); intAns =0; intLen =strlen (s); //equivalent to having n strings to compare and pick out the smallest dictionary order for(inti =1; i < Len; ++i) { if(Less (S, i, ans) = =1) {ans=i; } } //after looping the selection ans has saved the dictionary order the position of the first character of the smallest string for(inti =0; i < Len; ++i) {printf ("%c", s[(ans + i)%Len]); } printf ("\ n"); }//While (t) return 0;}
View Code
UVa 1584 Circular Sequence---water problem