POINT: 如何判断是否包含连续重复子串? 判断 当前串 的 后缀 啦~~~
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
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.
30 3 0 0
ABAC ABCA CBAB CABA CABC ACBA CABA 28
回溯比较有代表性的题+dfs搜素,刘汝佳AOAPCⅡ的例题,第一次比着标程抄的,当时理解了,后来发现回头再看还是没有头绪,事实证明果然还是没掌握。这是第二遍,方法细节要记住理解,希望第三次做时能顺利写出来。
没管格式,想直接贴代码的等着WA吧。╮(╯▽╰)╭
1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <cctype> 10 #include <algorithm> 11 #include <cmath> 12 #include <deque> 13 #include <queue> 14 #include <map> 15 #include <stack> 16 #include <list> 17 #include <iomanip> 18 using namespace std; 19 20 #define INF 0xffffff7 21 #define maxn 100000+10 22 int cnt; 23 int n, l; 24 int S[maxn]; 25 int dfs(int cur)//返回0表示已经得到解,无需继续搜索 26 { 27 if(cnt++ == n) 28 { 29 for(int i = 0; i < cur; i++) 30 printf("%c", 'A'+S[i]); 31 printf("\n"); 32 return 0; 33 } 34 for(int i = 0; i < l; i++) 35 { 36 S[cur] = i; 37 38 int ok = 1; 39 for(int j = 1; j*2 <= cur+1; j++)//尝试长度为j的后缀,后缀长度不能长于当前串的一半 40 { 41 int equal = 1; 42 for(int k = 0; k < j; k++) 43 { 44 if(S[cur-k] != S[cur-k-j]) 45 { 46 equal = 0; break;//若长度是j的后缀能形成困难的串,则j++,继续循环 47 } 48 } 49 if(equal) {ok = 0; break;}//反之,若能构成简单串,则直接退出循环; 50 } 51 //如果判断存在后缀能形成简单的串,则ok = 0,继续为S[cur]尝试下一个字符。 52 //反之,所有后缀都不会形成简单串,则继续下一位置(cur+1); 53 if(ok) 54 if(!dfs(cur+1)) return 0; 55 } 56 return 1; 57 } 58 59 60 int main() 61 { 62 scanf("%d%d", &n, &l); 63 dfs(0); 64 return 0; 65 }