hihoCoder 1014 Trie树 字典树模板

题目:

http://hihocoder.com/problemset/problem/1014

题意:

首先给出一些字符串,代表字典里的单词。然后再给出一些字符串,询问字典中以当前字符串为前缀的单词个数

思路:

字典树模板题。留个模板
动态字典树,特点是好写好理解,确定是效率慢,有可能TLE

//多实例记得调用trie_del函数清空字典树,单实例无所谓
#include 
using namespace std;

const int N = 100000 + 10, INF = 0x3f3f3f3f;
struct trie
{
    int val;
    trie *next[26];
    trie()
    {
        val = 0;
        memset(next, 0, sizeof next);
    }
}*root;
void trie_insert(char *s)
{
    trie *p = root;
    for(int i = 0; s[i]; i++)
    {
        int j = s[i] - 'a';
        if(p->next[j] == NULL) p->next[j] = new trie;
        p->next[j]->val++;
        p = p->next[j];
    }
}
int trie_query(char *s)
{
    trie *p = root;
    for(int i = 0; s[i]; i++)
    {
        int j = s[i] - 'a';
        if(p->next[j] == NULL) return 0;
        p = p->next[j];
    }
    return p->val;
}
void trie_del(trie *p)
{
    for(int i = 0; i < 26; i++)
        if(p->next[i] != NULL) trie_del(p->next[i]);
    delete p;
}
int main()
{
    int n, m;
    root = new trie;
    char s[20];
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%s", s);
        trie_insert(s);
    }
    scanf("%d", &m);
    for(int i = 1; i <= m; i++)
    {
        scanf("%s", s);
        printf("%d\n", trie_query(s));
    }
    trie_del(root);
    return 0;
}

静态字典树,特点是速度快,但需要的空间稍大,小心空间开小导致的RE

//这个模板长的很像动态字典树
#include 
using namespace std;

const int N = 100000 + 10;
struct node
{
    int val;
    node *next[26];
}trie[N*10], *root;
int tot;
char s[N];
node* node_init()
{
    trie[tot].val = 0;
    memset(trie[tot].next, 0, sizeof trie[tot].next);
    return trie + tot++;
}
void trie_insert(char *s)
{
    node *p = root;
    for(int i = 0; s[i]; i++)
    {
        int j = s[i] - 'a';
        if(p->next[j] == NULL) p->next[j] = node_init();
        p->next[j]->val++;
        p = p->next[j];
    }
}
int trie_query(char *s)
{
    node *p = root;
    for(int i = 0; s[i]; i++)
    {
        int j = s[i] - 'a';
        if(p->next[j] == NULL) return 0;
        p = p->next[j];
    }
    return p->val;
}
int main()
{
    int n, m;
    tot = 1;
    root = trie + 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%s", s);
        trie_insert(s);
    }
    scanf("%d", &m);
    for(int i = 1; i <= m; i++)
    {
        scanf("%s", s);
        printf("%d\n", trie_query(s));
    }
    return 0;
}
//多实例清空字典树时将sz初始化为1即可
#include 
using namespace std;

const int N = 100000 + 10, INF = 0x3f3f3f3f;
struct node
{
    int val;
    int next[26];
}trie[N*5];
int sz;

int node_init()
{
    memset(trie[sz].next, 0, sizeof trie[sz].next);
    trie[sz].val = 0;
    return sz++;
}
void trie_insert(char *s)
{
    int id = 0;
    for(int i = 0; s[i]; i++)
    {
        int j = s[i] - 'a';
        if(trie[id].next[j] == 0) trie[id].next[j] = node_init();
        trie[trie[id].next[j]].val++;
        id = trie[id].next[j];
    }
}
int trie_query(char *s)
{
    int id = 0;
    for(int i = 0; s[i]; i++)
    {
        int j = s[i] - 'a';
        if(trie[id].next[j] == 0) return 0;
        id = trie[id].next[j];
    }
    return trie[id].val;
}
int main()
{
    int n, m;
    char s[20];
    sz = 1;
    memset(trie[0].next, 0, trie[0].next);
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%s", s);
        trie_insert(s);
    }
    scanf("%d", &m);
    for(int i = 1; i <= m; i++)
    {
        scanf("%s", s);
        printf("%d\n", trie_query(s));
    }
    return 0;
}

你可能感兴趣的:(字典树)