hdu 1251 字典树入门

hdu 1251 统计难题

题意:

Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.

思路:

就是一个基础的字典树,,是一种树形结构。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串)。它的优点是:利用字符串的公共前缀来节约存储空间,最大限度地减少无谓的字符串比较,查询效率比哈希表高。
对于在一堆字符串中找某个字符串,或某些公共前缀的字符串,复杂度为O(l),l为字符串的长度。

如图就是一个字典树
然后就是根结点表示空字符串,每一条表表示根节点字符串后接一个新的字符,对应儿子就是接上该字符后的新字符串。
简单就是从根节点走到任意一点,路径上经过的边上字符就组成了该节点存的字符串。

一般,字典树比较习惯用指针搞;

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
struct node
{
    int account;
    node* child[26];
    node ()
    {
        account =0;
        for(int i=0;i<26;i++)
            child[i]=NULL;
    }
};
node *root=new node();
void inserts(char str[])
{
    int len=strlen(str);
    node* tmp=root;
    for(int i=0;i<len;i++)
    {
        int s=str[i]-'a';
        if(tmp->child[s]==NULL)
            tmp->child[s]=new node;
        tmp->account++;
        tmp=tmp->child[s];
    }
    tmp->account++;
}
int query(char str[])
{
    int len=strlen(str);
    node* tmp=root;
    for(int i=0;i<len;i++)
    {
        int s=str[i]-'a';
        if(tmp->child[s]==NULL)
            return 0;
        tmp=tmp->child[s];
    }
    return tmp->account;
}
int main()
{
    char str[20];
    while(gets(str),strcmp(str,""))
        inserts(str);
    while(gets(str)!=NULL)
        printf("%d\n",query(str));
    return 0;
}

顺便贴一个数组搞的字典树。

#include <cstdio>  
#include <cstring>  
using namespace std;  
const int maxn = 500000;  
const int maxw = 20;  
struct node {  
    bool f; //是否是单词  
    int child[26]; //是否出现字母  
    int cnt;  //前缀出现次数  
    int acnt; //单词出现次数  
    node() {  
        f=false;  
        cnt = 0;  
        acnt = 0;  
        memset(child, 0, sizeof(child));  
    }  
} T[maxn];  

char word[maxw];  
int sz =1; //结点总数  

void insert(char* s)  
{  
    int i, j, pos;  
    int len = strlen(s);  
    int u = 0;  
    for(i=0; i<len; ++i) {  
        pos = s[i]-'a';  
        if(0 == T[u].child[pos]) { //结点不存在  
            T[u].child[pos] = sz++; //新建结点  
        }  
        u = T[u].child[pos]; //往下走  
        T[u].cnt++;  
    }  
    T[u].f = 1;  
    T[u].acnt++;  
}  

int find(char* s)  
{  
    int len = strlen(s);  
    int i, pos;  
    int u = 0;  
    for(i=0; i<len; ++i) {  
        pos = s[i] -'a';  
        if(0 == T[u].child[pos]) {  
            return 0;  
        }  
        u = T[u].child[pos]; //往下走  
    }  
    return T[u].cnt;  
}  

int main()  
{  
    char word[maxw];  
    while(gets(word)) {  
        int len = strlen(word);  
        if(0 == len) break;  
        insert(word);  
    }  
    while(gets(word)) {  
        printf("%d\n", find(word));  
    }  
    return 0;  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值