ID codes ultraviolet (146) (returns the largest string whose Lexicographic Order is smaller than the current string)

Source: Internet
Author: User

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)

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.