0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 19:38:47 +00:00

H1001. 【模板】字典树 1 [50' Code]

https://hydro.ac/record/6158016697ec9cde15d81974
This commit is contained in:
Baoshuo Ren 2021-10-02 14:51:39 +08:00 committed by Baoshuo Ren
parent 122daa2ef8
commit 6475724605
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

48
Hydro/H1001/H1001.cpp Normal file
View 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;
}