-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.cpp
More file actions
62 lines (53 loc) · 1.17 KB
/
Copy pathtools.cpp
File metadata and controls
62 lines (53 loc) · 1.17 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
61
#include "tools.h"
#include "simpletools.h"
#include <iostream>
Node::Node(int size) {
lst = std::vector <Node *> (size, NULL);
cnt = 0;
}
Trie::Trie(int _size) {
size = _size;
root = new Node(size);
}
Trie::Trie() {root = NULL;}
static void clean_Trie(Node *root) {
if (root == NULL) return;
for (auto to : root->lst) {
clean_Trie(to);
}
delete root;
}
Trie::~Trie() {
clean_Trie(root);
}
void Trie::build(int _size) {
root = new Node(_size);
size = _size;
}
void Trie::insert(const std::vector <int> &string) {
Node *now = root;
for (int num : string) {
now->cnt++;
if (!now->lst[num]) {
now->lst[num] = new Node(size);
}
now = now->lst[num];
}
now->cnt++;
}
int Trie::get_number(const std::vector <int> &string) {
int result = 0;
Node *now = root;
for (int num : string) {
for (int i = 0; i < num; i++) {
result += (now->lst[i]) ? now->lst[i]->cnt : 0;
}
now = now->lst[num];
}
if (result >= MAXLEN) {
std::cout << result << '\n';
print_string(string);
exit(0);
}
return result;
}