-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
60 lines (50 loc) · 1.5 KB
/
main.cpp
File metadata and controls
60 lines (50 loc) · 1.5 KB
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
#include <iostream>
#include <functional>
#include <vector>
#include <string>
int main(int argc, const char** argv) {
if (argc < 3 || argc > 4) {
std::cerr << "Usage: " << argv[0] << " count alphabet [string]"
<< std::endl
<< std::endl
<< "If the optional string argument is used, the program will print "
"the position of the given string in the sequence"
<< std::endl;
return -1;
}
const bool gen = (argc == 3);
std::string alphabet(argv[2]);
if (alphabet == "AA")
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
else if (alphabet == "00")
alphabet = "0123456789";
if (alphabet.size() > 256)
throw std::invalid_argument("more than 256 chars in an alphabet is disallowed");
const size_t n = std::stoull(argv[1]);
const size_t k = alphabet.size();
std::vector<uint8_t> a(k * n, 0);
std::string ret;
std::function<void(size_t, size_t)> inner = [&](size_t t, size_t p) {
if (__builtin_expect(t <= n, 0)) {
a[t] = a[t - p];
inner(t + 1, p);
for (size_t i = a[t - p] + 1; i < k; ++i) {
a[t] = i;
inner(t + 1, t);
}
}
else if (__builtin_expect(!(n % p), 0)) {
auto begin = a.begin() + 1;
auto end = begin + p;
for (auto iter = a.begin() + 1; iter != end; ++iter)
ret.push_back(alphabet[*iter]);
}
};
inner(1, 1);
if (gen)
std::cout << ret << std::endl;
else {
std::cout << ret.find(argv[3]) << std::endl;
}
return 0;
}