UVa 129 Krypton Factor: Backtracking good questions

Source: Internet
Author: User
Tags time limit

129-krypton Factor

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=65

have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental a nd physical abilities. In one of the contest the contestants are tested on their ability to recall sequence a characters of which has bee n read to them by the quiz Master. Many of the contestants are very good at recognising patterns. Therefore, in order to add some difficulty to this test, the organisers have decided that sequences containing certain Es of repeated subsequences should not is used. However, they do not wish to remove all subsequences this are repeated, since in ' no single character could is re Peated. This in itself would make the problem too easy for the contestants. Instead it is decided to eliminate all sequences containing an occurrence of two adjoining identical. Sequences containing such an occurrence would be called ' easy '. Other sequences would be called ' hard '.

For example, the sequence Abacbcbad are easy, since it contains adjoining of the repetition CB. Other examples an easy sequences are:

Bb

Abcdacabcab

Abcdabcd

Some examples of hard sequences are:

D

DC

Abdab

Cbabcba

Input and Output

In order to provide the quiz Master and a potentially unlimited source of questions you are asked to write a Would read input lines that contain integers n and L, where n > 0 and L are in the range

, and for each input line prints out the nth hard sequence (composed of letters drawn to the the ' the ' I letters in the Alph ABET), in increasing alphabetical order (alphabetical ordering here corresponds to the normal ordering encountered in a Di Ctionary), followed (on "Next line") by the length of this sequence. The the sequence in this ordering is A. You may assume this for given N and L there does exist at least n hard sequences.

For example, with L = 3, the 7 hard sequences are:

A AB ABA ABAC abaca abacab Abacaba

As each sequence was potentially very long, split it into groups of four (4) characters separated by a. If There are more than such groups, please start a new line for the 17th group.

This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45370.htm

Therefore, if the integers 7 and 3 appear on a input line, the output lines produced should is

ABAC ABA
7

The Input is terminated by a line containing two zeroes. Your may assume a maximum sequence length of 80.

Sample Input

3
0 0

Sample Output

ABAC ABCA cbab caba cabc ACBA Caba
28

Tree-Type search. Backtracking.

Are written in the notes.

Complete code:

/*0.015s*/#include <cstdio> int n, L, CNT;  
    
int s[100];  
        int dfs (int cur)//return 0 means that the solution has been obtained without continuing to search {if (cnt++ = = N) {for (int i = 0; i < cur; i++)  {if (i) {if (i%) {if (i%)  
                4 = = 0) Putchar (");  
            else Putchar (10); } putchar (' A ' + s[i]);  
        Output scheme} printf ("\n%d\n", cur);  
    return 0;  
        for (int i = 0; i < L; i++) {s[cur] = i;  
        int OK = 1;  
            for (int j = 1; J * 2 <= cur + 1; j +)///Only the first half {int equal = 1;  
                    for (int k = 0; k < J; k++)/Check that the last half is equal to the first half if (S[cur-k]!= s[cur-k-j]) {  
                    equal = 0;  
                Break  
          } if (equal) {      OK = 0;  
            The latter half equals the first half, the scheme is not legal break; } if (ok &&!dfs (cur + 1)) return 0; Recursive search.  
If the solution has been found, simply exit} return 1;  
        int main () {while (scanf ("%d%d", &n, &l), n) {cnt = 0;  
    DFS (0);  
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.