UV-146-ID Codes (enumeration arrangement)

Source: Internet
Author: User

UV-146-ID Codes (enumeration arrangement)

UV-146

ID Codes
Time Limit:3000 MS   Memory Limit:Unknown   64bit IO Format:% Lld & % llu

 

Submit Status

Description

It is 2084 and the year of Big Brother has finally arrived, albeit a century late. in order to exercise greater control over its citizens and thereby to counter a chronic breakdown in law and order, the Government decides on a radical measure -- all citizens are to have a tiny microcomputer surgically implanted in their left wrists. this computer will contains all sorts of personal information as well as a transmitter which will allow people's movements to be logged and monitored by a central computer. (A desirable side effect of this process is that it will shorten the dole queue for plastic surgeons .)

 

An essential component of each computer will be a unique identification code, consisting of up to 50 characters drawn from the 26 lower case letters. the set of characters for any given code is chosen somewhat haphazardly. the complicated way in which the code is imprinted into the chip makes it much easier for the manufacturer to produce codes which are rearrangements of other codes than to produce new codes with a different selection of letters. thus, once a set of letters has been chosen all possible codes derivable from it are used before changing the set.

 

For example, suppose it is decided that a code will contain in exactly 3 occurrences'A', 2'BAnd 1'C', Then three of the allowable 60 codes under these conditions are:

 

      abaabc      abaacb      ababac

These three codes are listed from top to bottom in alphabetic order. Among all codes generated with this set of characters, these codes appear consecutively in this order.

 

Write a program to assist in the issuing of these identification codes. your program will accept a sequence of no more than 50 lower case letters (which may contain repeated characters) and print the successor code if one exists or the message 'No Successor' if the given code is the last in the sequence for that set of characters.

 

Input and Output

Input will consist of a series of lines each containing a string representing a code. The entire file will be terminated by a line consisting of a single#.

 

Output will consist of one line for each code read containing the successor code or the words'No Successor'.

 

Sample input

 

abaacbcbbaa#

 

Sample output

 

ababacNo Successor

 

Source

Root: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim): Chapter 2. data Structures and Libraries: Data Structures With Built-in Libraries: STL algorithm
Root: Competitive Programming 2: This increases the lower bound of Programming Contests. again (Steven & Felix Halim): Data Structures and Libraries: Linear Data Structures with Built-in Libraries: C ++ STL algorithm (Java Collections)
Root: aoapc I: Beginning Algorithm Contests (Rujia Liu): Volume 3. Brute Force: Elementary Skills
Root: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim): Data Structures and Libraries: Linear Data Structures with Built-in Libraries :: C ++ STL algorithm (Java Collections)

Submit Status


 

 

 

 

Idea: Use next_permutation in STL to generate the next sequence of the current sequence in the lexicographically ordered order. This can also be used for strings and for reentrant sets. Note: in this case, if the current sequence is the last one, No Successor is output. Therefore, before using next_permutation, you must determine whether the sequence is the last one.

 

AC code:

 

#include 
 
  #include 
  
   #include #include 
   
    using namespace std;char str[55];int fun(char a[]) {int len = strlen(a);for(int i = 0; i < len - 1; i++) {if(a[i] < a[i + 1]) return 1;}return 0;}int main() {while(scanf("%s", str), str[0] != '#') {int len = strlen(str);if(fun(str)) {next_permutation(str, str + len);printf("%s\n", str);}else printf("No Successor\n");}return 0;} 
   
  
 


 

 

Update: In the past, next_permutation itself can determine whether it is in the last lexicographically ordered sequence. Therefore, you can directly use next_permutation.

 

AC code:

 

#include 
 
  #include 
  
   #include #include 
   
    using namespace std;char str[55];int main() {while(scanf("%s", str), str[0] != '#') {int len = strlen(str);if(next_permutation(str, str + len))printf("%s\n", str);else printf("No Successor\n");}return 0;} 
   
  
 


 

 

 

 

 

 

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.