Trie·

Trie

const int maxnode=1e6+100;
const int sigma_size=26;
struct Trie
{
int ch[maxnode][sigma_size];//存储节点信息,构造为树,ch[i][j]表示第i个节点字母j的节点的位置
int val[maxnode];//规定非结尾节点的附加价值为0
int sz;//节点总数
void clear()
{
sz=1;
memset(ch[0],0,sizeof(ch[0]));
}
int idx(char c)
{
return c-'a';
}
void init(void)
{
memset(ch,0,sizeof(ch));
memset(val,0,sizeof(val));
}
//插入字符串S,附加信息为v,注意v必须非0,因为0代表“本节点不是单词节点”
void insert(char *s,int v)
{
int u=0,n=strlen(s);
for(int i=0;i<n;i++){
int c=idx(s[i]);
if(!ch[u][c]) //节点不存在
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;//中间节点的附加信息为0
ch[u][c]=sz++;//新建节点
}
u=ch[u][c];
}
val[u]=v;//字符串的最后一个字符的附加信息为v
//考虑是否去重
}
int query(char *s)
{
int u=0,n=strlen(s);
for(int i=0;i<n;i++){
int c=idx(s[i]);
if(!ch[u][c]) return 0;
u=ch[u][c];
}
return 1;
}
};
Trie trie;
int main()
{
   trie.clear();
   return 0;
}



posted @ 2022-02-27 14:02  fengzlj  阅读(107)  评论(0)    收藏  举报