Let's talk about:
The question is actually very simple. It is to give you a string consisting of lower-case English letters, and then ask you to find the largest string with a Lexicographic Order smaller than the current string. The solution is to traverse from the end of the string. If the obtained sub-string is already the minimum lexicographically obtained by the string, continue to traverse. Otherwise, first find the largest character smaller than the first character of the original string in the substring and swap the two. Then, sort the strings other than the first character to obtain the sub-string of the maximum Lexicographic Order. For more information, see the source code.
Source code:
# Include <stdio. h> # include <string. h> # define Max 50 + 5 char ID [Max]; int ismin (char *); // determines whether the current substring is in the smallest Lexicographic Order void getans (char *); // obtain the maximum string int main () {int I, Len, offset, no; char * P; // freopen ("data", "r ", stdin); While (scanf ("% s", ID) {If (ID [0] = '#') break; Len = strlen (ID ); no = 1; for (I = 2; I <= Len; I ++) {// traverse P = & ID [Len-I] from the end of the string; If (! Ismin (p) {getans (p); NO = 0; break ;}} if (NO) printf ("no successor \ n "); else printf ("% s \ n", ID);} return 0;} int ismin (char * P) {int I, Len; Len = strlen (P ); for (I = 1; I <Len; I ++) if (P [I]> P [I-1]) return 0; return 1;} void getans (char * P) {int I, Len, POs, J; char temp; Len = strlen (p); for (I = 1; I <Len; I ++) // obtain the IF (P [I]> P [0]) {pos = I; break;} for (I ++; I <Len; I ++) if (P [I] <p [POS] & P [I]> P [0]) // obtain the position Pos = I; temp = P [0]; P [0] = P [POS]; P [POS] = temp; P ++; Len --; for (I = 1; I <Len; I ++) // sort the substrings except the first character to obtain the maximum Lexicographic Order (j = 0; j <len-I; j ++) if (P [J]> P [J + 1]) {temp = P [J]; P [J] = P [J + 1]; P [J + 1] = temp;} return ;}
ID codes ultraviolet (146) (returns the largest string whose Lexicographic Order is smaller than the current string)