forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbing.cpp
45 lines (34 loc) · 755 Bytes
/
bing.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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct node {
int touched = 0;
vector<node*> next;
};
int insert(node* root, string s) {
int c = s.back() - 'a';
s.pop_back();
if(root->next[c] == nullptr) {
root->next[c] = new node;
root->next[c]->next.resize(26, nullptr);
}
int ans = root->next[c]->touched;
root->next[c]->touched++;
if(s.size() == 0) {
return ans;
}
return insert(root->next[c], s);
}
int main() {
int n;
cin >> n;
node* tree = new node;
tree->next.resize(26, nullptr);
while(n--) {
string s;
cin >> s;
reverse(s.begin(), s.end());
cout << insert(tree, s) << endl;
}
}