-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP129.cpp
91 lines (86 loc) · 2.21 KB
/
P129.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
bool check(char const * const s, const int size) {
if(s[size-1] == s[size-2]) {
///std::cerr << " "<< s << " TRIVIALLY BAD" << std::endl;
return false;
}
// check from right and go left... super fast!
char lastC = s[size-1];
for(int i = size-2; i >= 0; --i) {
if(s[i] != lastC) {
continue;
}
bool duplicate = true;
for(int j = 1; size-1-j > i; ++j) {
if(i-j < 0) {
///std::cerr << " EARLY " << s << std::endl;
return true;
}
if(s[size-1-j] != s[i-j]) {
duplicate = false;
break;
}
}
if(duplicate) {
///std::cerr << " " <<s << " contains duplicates" << std::endl;
return false;
}
}
///std::cerr << " LATE " << s << std::endl;
return true;
}
// return how many written.
void compute(char *cs, const int i, const int L, int &remaining) {
///std::cerr << "Running " << cs << ", i=" << i << ", L=" << L << ", rem=" << remaining << std::endl;
if(remaining == 0) {
return;
}
for(char l = 'A'; l < 'A'+L; ++l) {
cs[i] = l;
cs[i+1] = '\0';
///std::cerr << " Test " << cs << ", i=" << i << ", L=" << L << ", rem=" << remaining << std::endl;
if(check(cs, i+1)) {
--remaining;
if(remaining == 0) {
///std::cerr << " Last check OK " << cs << ", i=" << i << ", L=" << L << ", rem=" << remaining << std::endl;
return;
}
compute(cs, i+1, L, remaining);
if(remaining == 0) {
///std::cerr << " RECURSE => OK " << cs << ", i=" << i << ", L=" << L << ", rem=" << remaining << std::endl;
return;
}
}
///std::cerr << " DONE Test " << cs << ", i=" << i << ", L=" << L << ", rem=" << remaining << std::endl;
cs[i] = '\0';
}
}
void output(char const * const s) {
int i = 0;
for(; s[i] != '\0'; ++i) {
if(i > 0 && i % 4 == 0) {
if(i % 64 == 0) {
std::cout << std::endl;
}
else {
std::cout << ' ';
}
}
std::cout << s[i];
}
std::cout << std::endl << i << std::endl;
}
int main() {
while(true) {
// Read input
int n, L;
std::cin >> n >> L;
if(L == 0)
return 0;
///std::cerr << "RUNNING n " << n << " L " << L << std::endl;
// Set up:
char cs[100];
compute(cs, 0, L, n);
output(cs);
}
}