Problem
问题
You have been employed by the organisers of a Super Krypton 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. 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 types of repeated subsequences should not be used. However, they do not wish to remove all subsequences that are repeated, since in that case no single character could be repeated. 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 subsequences. Sequences containing such an occurrence will be called “easy”. Other sequences will be called “hard”.
“超级氪因素大赛”(译注:英国的一档电视心智竞答节目)的主办方雇你来对付那些足智多谋的参赛选手。在比赛的一个环节中,节目主持人将念出一长串的字母来考验选手的记忆能力。因为许多选手都是分析字串模式的高手,为了增加一些比赛的难度,主办方决定不再使用那些含有特定重复子串的字串。但是他们又不能将所有重复的子串都删掉,如果那样的话字串中就不存在两个相同的单字了,这反倒会让问题变的非常简单。为了解决这一问题,他们决定仅删除那些包含相邻重复子串的字串。我们将存在上述相邻重复情况的字串称为“easy”(简单),否则称为“hard”(难)。
For example, the sequence ABACBCBAD is easy, since it contains an adjoining repetition of the subsequence CB. Other examples of easy sequences are:
比方说,字串ABACBCBAD就是一个“easy”,因为它里面有相邻的重复子串“CB”。另举一些“easy”字串的例子如下:
- BB
- ABCDACABCAB
- ABCDABCD
Some examples of hard sequences are:
下面是一个“hard”字串的例子:
- D
- DC
- ABDAB
- CBABCBA
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 A. You may assume that for given n and L there do exist at least n hard sequences.
为了能给节目主持人提供无限量的问题字串,要求你来写一个程序执行生成运算。程序从输入中读取多行数据,每行包括两个整数n和L(即按此顺序给出),其中n > 0,L的范围是1 ≤ L ≤ 26。根据这些输入,程序要按照字母表升序打印出第n个“hard”字串(由字母表中的前L个字母构成),并在接下来的一行打印这个串的长度。按照上述规则,第一个串应该是“A”。对于给定的n和L,你可以认为第n个“hard”串是一定存在的。
For example, with L = 3, the first 7 hard sequences are:
比方说,当L = 3时,头7个“hard”字串为:
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.
字串可能很长,因此要将它们分成4个字为一组,中间用空格隔开。如果超过16组,则换一行,再接着输出第17组。
Therefore, if the integers 7 and 3 appear on an input line, the output lines produced should be:
按照此格式,对于输入的整数7和3,输出应该为:
ABAC ABA
7
Input is terminated by a line containing two zeroes. Your program may assume a maximum sequence length of 80.
输入由一行两个零表示结束。你的程序可以限定最大的字串长度为80。
Sample Input
输入示例
30 3
0 0
Sample Output
输出示例
ABAC ABCA CBAB CABA CABC ACBA CABA
28
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 100;
char a[maxn];
int n,l,cnt;
int dfs(int cur){
if(cnt++ == n){
for(int i=0;i<cur;i++){
if(i%64==0 && i) cout<<"\n";
else if(i%4==0 && i) cout<<" ";
printf("%c",a[i]+'A');
}
cout<<"\n";
cout<<cur<<"\n";
return 0;
}
for(int i=0;i<l;i++){
a[cur]=i;
int ok=1;
for(int j=1;j*2<=cur+1;j++){
int equal=1;
for(int k=0;k<j;k++)
if(a[cur-k]!=a[cur-k-j]) {equal=0;break;}
if(equal) {ok=0;break;}
}
if(ok) if(!dfs(cur+1)) return 0;
}
return 1;
}
int main()
{
while(cin>>n>>l)
{
if(n == 0&& l == 0)
break;
memset(a,0,sizeof(a));
cnt = 0;
dfs(0);
}
return 0;
}