You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
876 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}