字典树模板

来源:互联网 发布:易名中国域名管理系统 编辑:程序博客网 时间:2024/06/04 05:49

动态树

code

#include <iostream>using namespace std;const int MAXM = 30,KIND = 26;int m;struct node{    char* s;    int prefix;    bool isword;    node* next[KIND];    node()    {        s = NULL;        prefix = 0;        isword = false;        memset(next,0,sizeof(next));    }}*root;//根void insert(node *root,char *s)//插入{    node *p = root;    for (int i = 0;s[i];i++)    {        int x = s[i] - 'a';        p->s = s+i;        if (p->next[x] == NULL)            p->next[x] = new node;        p = p->next[x];        p->prefix++;    }    p->isword = true;}bool del(node *root,char *s)//删除{    node *p = root;    for (int i = 0;s[i];i++)    {        int x = s[i] - 'a';        if (p->next[x] == NULL)            return false;        p = p->next[x];    }    if (p->isword)        p->isword = false;    else        return false;    return true;}bool search(node *root,char* s)//查找s这个字符串,是否出现过{    node* p = root;    for (int i = 0;s[i];i++)    {        int x = s[i]-'a';        if (p->next[x] == NULL)            return false;        p = p->next[x];    }    return p->isword;}int count(node *root,char *s)//统计以s作为前缀的字符串的个数{    node *p = root;    for (int i = 0;s[i];i++)    {        int x = s[i] - 'a';        if (p->next[x] == NULL)            return 0;        p = p->next[x];    }    return p->prefix;}int main(){    m = 0;    root = new node;    char s[MAXM];    while (gets(s))    {        if (strcmp(s,"") == 0)            break;        insert(root,s);    }    while (gets(s))        printf("%d\n",count(root,s));}

///静态trie

code

#include <iostream>using namespace std;const int MAXN = 100010,MAXM = 30,KIND = 26;int m;struct node{    char* s;    int prefix;    bool isword;    node* next[KIND];    void init()    {        s = NULL;        prefix = 0;        isword = false;        memset(next,0,sizeof(next));    }}a[MAXN*MAXM],*root;//根void insert(node *root,char *s){    node *p = root;    for (int i = 0;s[i];i++)    {        int x = s[i] - 'a';        p->s = s+i;        if (p->next[x] == NULL)        {            a[m].init();            p->next[x] = &a[m++];        }        p = p->next[x];        p->prefix++;    }    p->isword = true;}bool del(node *root,char *s){    node *p = root;    for (int i = 0;s[i];i++)    {        int x = s[i] - 'a';        if (p->next[x] == NULL)            return false;        p = p->next[x];    }    if (p->isword)        p->isword = false;    else        return false;    return true;}bool search(node *root,char* s){    node* p = root;    for (int i = 0;s[i];i++)    {        int x = s[i]-'a';        if (p->next[x] == NULL)            return false;        p = p->next[x];    }    return p->isword;}int count(node *root,char *s){    node *p = root;    for (int i = 0;s[i];i++)    {        int x = s[i] - 'a';        if (p->next[x] == NULL)            return 0;        p = p->next[x];    }    return p->prefix;}int main(){    m = 0;    a[m].init();    root = &a[m++];    char s[MAXM];    while (gets(s))    {        if (strcmp(s,"") == 0)            break;        insert(root,s);    }    while (gets(s))        printf("%d\n",count(root,s));}


原创粉丝点击