字典树是一种用于实现字符串快速检索的多叉树结构
int n, m, ans, tot, p;
int trie[N][26], cnt[N];
bool exist[N];//该结点结尾的字符串是否存在
string s, t;
inline void t_insert(string s)//插入一个字符串
{
int len = s.size(), p = 0;//初始化p
for (int i = 0; i < len; i++)
{
int c = s[i] - 'a';
if (!trie[p][c])
trie[p][c] = ++tot; //如果没有,就添加结点
p = trie[p][c]; //更改指针p的指向
}
exist[p] = 1;
}
inline bool t_query(string s)//检索字符串是否存在
{
int len = s.size(), p = 0;
for (int i = 0; i < len; i++)
{
int c = s[i] - 'a';
if (!trie[p][c])//说明s没有被插入过trie,结束检索
return 0;
p = trie[p][c]; //更改指针p的指向
}
return exist[p]; //s存在
}