推荐几篇博客:https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 浅谈Trie树
https://blog.csdn.net/u013588639/article/details/38406453 字典树的数组实现
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn=200010; 6 int ch[maxn][27],val[maxn]; 7 int sz; 8 9 void init() 10 { 11 sz=0; 12 memset(ch,0,sizeof(ch)); 13 memset(val,0,sizeof(val)); 14 } 15 16 void insert(char *s) 17 { 18 int u=0,c; 19 for ( int i=0;s[i];i++ ) 20 { 21 c=s[i]-'a'; 22 if ( !ch[u][c] ) ch[u][c]=++sz; 23 u=ch[u][c]; 24 val[u]++; 25 } 26 } 27 28 int query(char *s) 29 { 30 int u=0,c; 31 for ( int i=0;s[i];i++ ) 32 { 33 c=s[i]-'a'; 34 if ( !ch[u][c] ) return 0; 35 u=ch[u][c]; 36 } 37 return val[u]; 38 }