Krypton Factor

ou have been employed by the organisers of aSuper Krypton Factor Contest in which contestants have very high mental andphysical abilities. In one section of the contest the contestants are tested ontheir ability to recall a sequence of characters which has been read to them bythe Quiz Master. Many of the contestants are very good at recognising patterns.Therefore, in order to add some difficulty to this test, the organisers havedecided that sequences containing certain types of repeated subsequences shouldnot be used. However, they do not wish to remove all subsequences that arerepeated, since in that case no single character could be repeated. This initself would make the problem too easy for the contestants. Instead it isdecided to eliminate all sequences containing an occurrence of two adjoiningidentical 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 examplesof 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 with apotentially unlimited source of questions you are asked to write a program thatwill read input lines that contain integers n and L (inthat order), where n > 0 and L is in therange  , and foreach input line prints out the nth hard sequence (composed ofletters drawn from the first L letters in the alphabet), inincreasing alphabetical order (alphabetical ordering here corresponds to thenormal ordering encountered in a dictionary), followed (on the next line) bythe length of that sequence. The first sequence in this ordering is A. You mayassume that for given n and L there do existat least n hard sequences.

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


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 aremore than 16 such groups, please start a new line for the 17th group.

Therefore, if the integers 7 and 3 appear onan input line, the output lines produced should be

ABAC ABA

7

Input is terminated by a line containing twozeroes. Your program may assume a maximum sequence length of 80.

Sample Input

30 3

0 0

Sample Output

ABAC ABCA CBAB CABA CABC ACBA CABA

28

跟八皇后问题差不多,只需判断当前加入的这个字符是否符合题目要求即可。也就是加上这个字符之后判断是否有相邻子串相等。需要注意几点:

1.所要求的是第n个串,而第n个串不一定包含n个字符。因为hard字符串按照深搜来形成的,当n+1(假设是第i个hard串)位置放置任何字符都是easy时,不会再继续加字符了,而是把n处的字符换掉,这时第i+1hard串长度和第i个是相等的。

2.基于“1”,那么我们需要一个cnt来记录这是第几个hard串,而cur仍然保存字符串的长度。

3.注意输出的格式

代码如下:

#include<cstdio>
using namespace std;

const int maxn=10000+5;
int seq[maxn],L,n,cnt;

int dfs(int cur){
	if(cnt++==n){
		for(int i=0;i<cur;i++){
			if(i%4==0&&i!=0&&i!=64) printf(" ");
			printf("%c",seq[i]+'A');
			if((i+1)%64==0) printf("\n");
		}
		if(cur!=64) printf("\n");
		printf("%d\n",cur);
		return 0;
	}
	for(int i=0;i<L;i++){
		seq[cur]=i;
		bool ok=true;
		for(int j=1;j*2<=cur+1;j++){
			bool equal=1;
			for(int k=0;k<j;k++)
				if(seq[cur-k]!=seq[cur-j-k]){
					equal=0;
					break;
				}
			if(equal==1){
				ok=false;
				break;
			}
		}
		if(ok){
			if(!dfs(cur+1)) return 0;
		}
	}
	return 1;
}
int main(){
	while(scanf("%d%d",&n,&L)==2&&n&&L){
		cnt=0;
		dfs(0);
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值