-
Notifications
You must be signed in to change notification settings - Fork 7
/
numberofnodesintrie.cpp
60 lines (49 loc) · 1.09 KB
/
numberofnodesintrie.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
Problem : https
: // www.hackerearth.com/practice/data-structures/advanced-data-structures/trie-keyword-tree/practice-problems/algorithm/cost-of-data-11/
#include <bits/stdc++.h>
#define int long long
#define sz(x) (int)(x.size())
using namespace std;
class TRIE {
private:
typedef struct node {
node *bit[26];
int cnt = 0;
} * nodeptr;
nodeptr root;
public:
int totalNodes;
TRIE() {
root = new node();
totalNodes = 1;
}
void insert(string val) {
nodeptr curr = root;
for (int i = 0; i < sz(val); i++) {
int currBit = val[i] - 'a';
if (curr->bit[currBit] == NULL)
curr->bit[currBit] = new node(), totalNodes++;
curr = curr->bit[currBit];
curr->cnt++;
}
}
int query() { return totalNodes; }
};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
TRIE T;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
T.insert(s);
}
cout << T.query() << '\n';
}