Uva oj 129-kryton factor (Krypton factor)

Source: Internet
Author: User

Time Limit: 3.000 seconds
Time Limit: 3.000 seconds

Problem
Problem

You have been employed by the organisers of a super kryton factor contest in which contestants have very high mental and physical abilities. in one section of the contest the contestants are tested on their ability to recall a sequence of characters which has been read to them by the quiz master. parameters of the contestants are very good at recognizing patterns. therefore, in order to add some difficulty to this test, the organisers have decided that sequences containing certain types of repeated subsequences shocould not be used. however, they do not wish to remove all subsequences that are repeated, since in that case no single character cocould be repeated. this in itself wocould make the problem too easy for the contestants. instead it is decided to eliminate all sequences containing an occurrence of two adjoining identical subsequences. sequences containing such an occurrence will be called "easy ". other sequences will be called "hard ".
The English TV mental quiz contest Hired by the organizer You are here to deal with the many smart contestants. During the competition, the host will read a long string of letters to test the player's memory. Because many contestants are experts in the string analysis mode, in order to increase the difficulty of some competitions, the organizer decided not to use strings containing specific replay substrings. However, they cannot delete all repeated substrings. If so, there will be no two identical single words in the string, which makes the problem quite simple. To solve this problem, they decided to delete only strings that contain adjacent duplicate substrings. We call the strings with the preceding adjacent duplicates "easy" (simple), or "hard" (difficult ).

For example, the sequence abacbcbad is easy, since it contains an adjoining repetition of the subsequence CB. Other examples of easy sequences are:
For example, the string abacbcbad is an "easy" because it contains an adjacent duplicate substring "CB ". The following is an example of the "easy" string:

    • Bb
    • Abcdacabcab
    • Abcdabcd

Some examples of hard sequences are:
The following is an example of a "hard" string:

    • D
    • DC
    • Abdab
    • Cbabcba

 

Input and Output
Input and Output

In order to provide the quiz master with a potentially unlimited source of questions you are asked to write a program that will read input lines that contain integers n and L (in that order ), where n> 0 and L is in the range 1 ≤ L ≤ 26, and for each input line prints out the nth hard sequence (composed of letters drawn from the first l letters in the alphabet ), in increasing alphabetical order (alphabetical ordering here corresponds to the normal ordering encountered in a dictionary), followed (on the next line) by the length of that sequence. the first sequence in this ordering is. you may assume that for given n and l there do exist at least N hard sequences.
To provide unlimited problem strings to the program host, Requirements You can writeProgramExecute the generation operation. The program reads multiple rows of data from the input. Each row contains two integers n and L (given in this order). Where n> 0, the range of L is 1 ≤ L ≤ 26. Based on these inputs, the program prints the nth "hard" string (consisting of the first l letters in the alphabet) in ascending alphabetical order ), and print the length of the string in the next line. According to the above rules, the first string should be "". For the given n and l, you can think that the nth "hard" string exists.

For example, with L = 3, the first 7 hard sequences are:
For example, when l = 3, the first seven "hard" strings are:

A
AB
ABA
ABAC
Abaca
Abacab
Abacaba

As each sequence is potentially very long, split it into groups of four (4) characters separated by a space. if there are more than 16 such groups, please start a new line for the 17th group.
The strings may be very long. Therefore, you need to divide them into four characters and separate them with spaces. If there are more than 16 groups, change the line and then output the 17th group.

Therefore, if the integers 7 and 3 appear on an input line, the output lines produced shoshould be:

In this format, for the input integers 7 and 3, the output should be:

ABAC ABA
7

Input is terminated by a line containing two zeroes. Your program may assume a maximum sequence length of 80.
The input end with two zeros in one row. Your program can limit the maximum String Length to 80.

 

Sample Input
Input example

30 3
0 0

Sample output
Output example

Abac abca cbab Caba cabc acba Caba
28

 

Analysis
Analysis

This is a classic recursive call process with moderate difficulty. We recommend that you carefully repeat the analysis. The question itself is not very understandable, so you need to think carefully to understand its intention.

It is easy to avoid adjacent duplicate substrings: If the generated strings are added one by one after the strings, you only need to check whether the current string meets the conditions after each addition. If this parameter is not met, it cannot be added. This ensures that there are no adjacent repeated substrings in the string. The check method is to start from 1, check whether the last one character and the first one are equal, and then increase the length to 2, check whether the last two characters and the first two are equal, and so on.

The question must be generated in alphabetical order. The example (7 3) provided by the query shows that all strings start with a and are generated one by one. Each time you add a letter that meets the requirements as far as possible (smaller in the ASCII table) at the end. If none of the requirements are met, go back and try again by increasing the previous letter. Each successful addition is counted as generating a string. Until the number of generated strings reaches the specified number n.

After clear thinkingCodeIt is easy to write. It is a small program. Note that the output must follow the required format. A space should be added for every four characters. A carriage return should be added instead of a space after a 4 × 16 character. Add a space for every four digits. 10 million notes!

 

Solution
Answer
# Include <iostream> # include <string> using namespace STD; // recursively generates all strings in sequence. STR saves the result. N and l are the variables with the same name in the question void sequence (string & STR, Int & N, int L) {// record String Length, to speed up int nlen = Str. length (), nhalf = (Str. length () + 1)/2; // after the result string, try to add the first l uppercase letters for (char I = 'A', iend = L + 'A ', M = 1; I <iend; ++ I) {Str. push_back (I); // Insert the current character // check whether there are adjacent duplicates in the new string // 1st times to judge the last character, judge the last 2 Characters for 2nd times, and so on (M = 1; m <= nhalf; ++ m) {// compare the last I characters with the previous I characters. end ()-M, str. end (), str. end ()- M * 2) {m = 0; // set m to 0 to indicate that there are duplicate adjacent substrings break; // skip loop} If (M! = 0) {// if there are no duplicates // If the generated strings are sufficient, return the upper-level if (-- N = 0) return; sequence (STR, N, L ); // go to the next-level call // if the number of generated strings is sufficient, return the upper-level if (n = 0) return;} // Delete the character just added after, keep the original STR when the result string enters this level. erase (nlen); // prepare for adding the next character} // main function int main (void) {for (INT n, l; cin> N> L & n! = 0;) {// read the input string str of each group cyclically; // result string sequence (STR, N, L ); // recursively generate all non-adjacent repeated strings int nlen = Str. length (); // retain the number of characters in the generated string for (size_t I = 4; I <Str. length (); I + = 5) {// format-based processing string // insert a space at every four, insert a carriage return STR at every 80 characters. insert (Str. begin () + I, I = 79? '\ N': '');} // number of output strings and characters cout <STR <' \ n' <nlen <Endl;} 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.