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 sequenace 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''.
For example, the sequence ABACBCBAD is easy, since it contains an adjoining repetition of the subsequence CB. Other examples of easy sequences are:
BB
ABCDACABCAB
ABCDABCD
Some examples of hard sequences are:
D
DC
ABDAB
CBABCBA
For example, the sequence ABACBCBAD is easy, since it contains an adjoining repetition of the subsequence CB. Other examples of easy sequences are:
BB
ABCDACABCAB
ABCDABCD
Some examples of hard sequences are:
D
DC
ABDAB
CBABCBA
For example, with L = 3, the first 7 hard sequences 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.
Therefore, if the integers 7 and 3 appear on an input line, the output lines produced should be
ABAC ABA
7
Input is terminated by a line containing two zeroes. Your program may assume a maximum sequence length of 80.
ABAC ABCA CBAB CABA CABC ACBA CABA 28
30 3 0 0
ABAC ABCA CBAB CABA CABC ACBA CABA 28
题目啥意思呢:
输入n,L
L表示前L个字母,如果L等于3,则用字母ABC
n表示通过一定操作的字典序,啥操作:任意的一串字符串不含两个相同的子串。
这里我们可以使用后缀来写,因为插入一个字母,只需判断插入后含该字母的子串
#include <iostream>
using namespace std;
const int maxn=100;
char s[maxn];
int n,l;
int ans=0;
bool check(char *s1,int len)
{
//长度
for(int i=1;i<=len/2;i++){
bool flag=0;
//
for(int j=len;j>=len-i+1;j--){
if(s1[j]!=s1[j-i]) {
flag=true;
break;
}
}
if(flag==0)return false;
}
return true;
}
void dfs(int cur)
{
if(ans==n){
for(int i=1;i<cur;i++){
cout<<s[i];
if(i%64==0&&i!=(cur-1)){
cout<<endl;
continue;
}
if(i%4==0&&i!=(cur-1))cout<<" ";
}
cout<<endl;
cout<<cur-1<<endl;
}
if(ans>n)return;
for(int i=1;i<=l;i++){
char cur_c=i+'A'-1;
s[cur]=cur_c;
if(check(s,cur)){
ans++;
// cout<<"递归 "<<cur_c<<endl;
dfs(cur+1);
}
}
}
int main()
{
while(cin>>n>>l){
ans=0;
if(n==0&&l==0)break;
dfs(1);
}
return 0;
}