UVA - 129 Krypton Factor (回溯)


 Krypton Factor 

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''.

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

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 , 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.

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.

Sample Input

30 3
0 0

Sample Output

ABAC ABCA CBAB CABA CABC ACBA CABA
28


题目大意:如果一个字符串包含两个相邻的重复子串串,则称它是“容易的串”,其他串称为“困难的串”。

输入正整数n和 l,输出由前 l 个字符组成、字典序第n小的困难的串。


解析:这题和8皇后问题的解法差不多,只要判断当前串的后缀加入后,是否前后两段相同,如果不相同则可以继续添加,否则return ;

注意最后要每4个字符输出一个空格,每64个字符输出一个回车,最后输出字符串的长度。


#include 
int S[100];
int n,l;
int cnt;
int dfs(int cur) {
	if(cnt++ == n) { //递归边界
		for(int i = 0; i < cur; i++) {
			if(i) {
				if( i % 64) {
					if( i % 4 == 0) {
						printf(" ");
					}
				}else {
					printf("\n");
				}
			}
			printf("%c",'A'+S[i]); //输出方案
		}
		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++) { //尝试j*2的后缀
			int equal = 1;

			for(int k = 0; k < j; k++) {
				if( S[cur-k] != S[cur-k-j]) {
					equal = 0;
					break;
				}
			}
			if(equal) {
				ok = 0;
				break;
			}
		}
		if(ok && !dfs(cur+1)) {
			return 0;
		}
	}
	return 1;
}
int main() {
	while(scanf("%d%d",&n,&l) != EOF && (n || l) ) {
		cnt = 0;
		dfs(0);
	}
	return 0;
}


你可能感兴趣的:(回溯)