you has been employed by the organisers of a Super krypton Factor Contest I n which contestants has very high mental and physical abilities. In one section of the contest the contestants is tested on their ability to recall a sequence of characters which have bee n read to them by the Quiz Master. Many of the contestants is very good at recognising patterns. Therefore, in order to add some difficulty to this test, the organisers has decided that sequences containing certain typ Es of repeated subsequences should not be used. However, they do not wish to remove all subsequences that is repeated, since in the case no single character could is re Peated. This on itself would make the problem too easy for the contestants. Instead it is decided to eliminate all sequences containing an occurrence of both adjoining identical subsequences. Sequences containing such an occurrence would be called "easy". Other sequences would be called "hard".
For example, the sequence Abacbcbad was easy and since it contains an adjoining repetition of the subsequence CB. Other examples of easy sequences is:
Some examples of hard sequences is:
Input and Output
in order to provide the Quiz Master with a potentially unlimited source of Q Uestions you is asked to write a program that would read input lines that contain integers n and < Em>l (in this order), where n > 0 and L is in the range , and for each input line prints out the n th hard sequence (composed of letters drawn from the First l letters in the alphabet), with increasing alphabetical order (alphabetical ordering here corresponds to the No Rmal ordering encountered in a dictionary), followed (on the next line) by the length of this sequence. The first sequence in this ordering is A. Assume. given n and L there do exist at least N hard sequences.
For example, with L = 3, the first 7 hard sequences is:
A
Ab
ABA
Abac
Abaca
Abacab
Abacaba
As each sequence are potentially very long, split it into groups of four (4) characters separated by a space. If there is more than-such groups, please start a new line for the 17th group.
Therefore, if the integers 7 and 3 appear on a input line, the output lines produced should is
ABAC ABA7
Input is terminated by a line containing the zeroes. Your program may assume a maximum sequence length of 80.
Sample Input
30 30 0
Sample Output
ABAC ABCA cbab CABA cabc ACBA CABA28
The basic idea is to generate one letter at a time, just to make a decision on whether or not to meet the requirements. One idea is to judge whether or not the substrings of the even length of the left and right are meeting the requirements for each additional one, doing a lot of repetitive work. So each time the decision is only to determine the suffix even length of the substring to meet the requirements, and then back to solve.
AC Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring > #include <string> #include <sstream> #include <vector> #include <set> #include <map># Include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert > #include <cmath> #include <functional>using namespace Std;int N, L, cnt;int s[85];bool dfs (int cur)//return F Alse indicates that the solution has been obtained without continuing to search {if (cnt++ = = N) {for (int i = 0; i < cur; i++) {if (i% = = 0 && i > 0) {//64 per line cout << Endl;} else if (i% 4 = = 0 && i > 0) {//4 a space cout << ';} cout << (char) (' A ' + s[i]);} cout << endl << cur << endl;return false;} for (int i = 0; i < L; i++) {S[cur] = i;bool OK = true;for (int j = 1; J * 2 <= cur + 1; j + +) {//length j*2 suffix satisfies bool equal = true;for (int k = 0; k < J; k++) {if (s[cur-k]! = s[cur-k-J]) {//check if half equals first half equal = False; break;}} if (equal) {//is equal before and after, illegal OK = False;break;}} if (OK) {if (!dfs (cur + 1)) {return false;//recursive search, if found, exits}}}}int main () {Ios::sync_with_stdio (false); while (Cin >> ; n >> L && n) {cnt = 0;dfs (0);} return 0;}
Uva-129-krypton Factor