mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-23 21:28:48 +00:00
H1001. 【模板】字典树 1 [50' Code]
https://hydro.ac/record/6158016697ec9cde15d81974
This commit is contained in:
parent
122daa2ef8
commit
6475724605
48
Hydro/H1001/H1001.cpp
Normal file
48
Hydro/H1001/H1001.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct node {
|
||||
int size;
|
||||
node* next[26];
|
||||
|
||||
~node() {
|
||||
for (auto& i : next) {
|
||||
delete i;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void insert(node* root, string s) {
|
||||
if (s.empty()) {
|
||||
root->size = 1;
|
||||
return;
|
||||
}
|
||||
if (root->next[s[0] - 'a'] == nullptr) root->next[s[0] - 'a'] = new node();
|
||||
insert(root->next[s[0] - 'a'], s.substr(1));
|
||||
}
|
||||
|
||||
void calcsize(node* root) {
|
||||
for (int i = 0; i < 26; i++) {
|
||||
if (root->next[i] != nullptr) {
|
||||
calcsize(root->next[i]);
|
||||
root->size += root->next[i]->size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
int n;
|
||||
string s;
|
||||
node* root = new node();
|
||||
cin >> n;
|
||||
while (n--) {
|
||||
cin >> s;
|
||||
insert(root, s);
|
||||
}
|
||||
calcsize(root);
|
||||
cout << root->size << endl;
|
||||
delete root;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user