字典树

int trie[SIZE][26], tot=1;//初始化,假设字符串由小写字母构成
bool ed[SIZE];
void ins(char *str)
{
    int len=strlen(str),p=1;
    for(int k=0;k<len;k++)
    {
        int ch=str[k]-'a';
        if(trie[p][ch]==0) trie[p][ch]=++tot;
        p=trie[p][ch];
    }
    ed[p]=true;
}
bool sea(char *str)
{
    int len=strlen(str),p=1;
    for(int k=0;k<len;k++)
    {
        int ch=str[k]-'a';
        p=trie[p][ch];
        if(p==0) return false;
    }
    return ed[p];
}
posted @ 2020-01-18 19:39  ChildeZhe  阅读(131)  评论(0)    收藏  举报