Files
python/TangDou/AcWing/Trie/835_0.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

51 lines
876 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int tr[N][26];
int cnt[N];
int idx;
// 插入
void insert(string str) {
int p = 0; // 游动的节点编号
for (int i = 0; i < str.size(); i++) {
int u = str[i] - 'a'; // 边
if (!tr[p][u]) tr[p][u] = ++idx;
p = tr[p][u];
}
cnt[p]++;
}
/**
测试用例:
8
abcdef
abdef
aced
bcdf
bcff
cdaa
abc
bcdc
*/
int main() {
int n;
cin >> n;
while (n--) {
// 输入的字符串
string str;
cin >> str;
// 插入到Trie树
insert(str);
}
printf("Node Count:%d\n", idx);
for (int i = 0; i <= idx; i++)
for (int j = 0; j < 26; j++)
if (tr[i][j])
printf("%d-> %c -> %d\n", i, 'a' + j, tr[i][j]);
return 0;
}