'commit'
This commit is contained in:
51
TangDou/AcWing/Trie/835_0.cpp
Normal file
51
TangDou/AcWing/Trie/835_0.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user