forked from PuzzleTechHub/nutrimatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-anagrams.cpp
74 lines (62 loc) · 1.61 KB
/
find-anagrams.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
#include "index.h"
#include "search.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class AnagramFilter: public SearchFilter {
public:
AnagramFilter(char const* letters) {
for (size_t i = 0; i < sizeof(count) / sizeof(State); ++i) count[i] = 0;
while (*letters) ++count[(unsigned char) *letters++];
product = 1;
for (size_t i = 0; i < sizeof(count) / sizeof(State); ++i) {
if (0 == count[i]) {
value[i] = 0;
} else if (product * count[i] < product) {
fputs("anagram too long\n", stderr);
exit(1);
} else {
++count[i];
value[i] = product;
product *= count[i];
count[i] *= value[i];
}
}
}
bool is_accepting(State state) const {
return (state == product);
}
bool has_transition(State from, char ch, State* to) const {
if (ch == ' ') {
*to = (from == product - 1) ? product : from;
return true;
}
State v = value[(unsigned char) ch];
if (v == 0) return false;
State next = from + v;
if (next % count[(unsigned char) ch] < v) return false;
*to = next;
return true;
}
private:
State count[256];
State value[256];
State product;
};
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "usage: %s input.index letters\n", argv[0]);
return 2;
}
FILE *fp = fopen(argv[1], "rb");
if (fp == NULL) {
fprintf(stderr, "error: can't open \"%s\"\n", argv[1]);
return 1;
}
IndexReader reader(fp);
AnagramFilter filter(argv[2]);
SearchDriver driver(&reader, &filter, 0, 1e-6);
PrintAll(&driver);
return 0;
}