字典树基础篇一(hdu 1251)

该博客主要介绍了如何使用字典树基础理论来解决HDU 1251题目,即根据给定的前缀查询与之匹配的单词数量。文章内容包括题目的描述、解决方案以及相关的代码实现。
摘要由CSDN通过智能技术生成

题目:hdu 1251

题意:给定一堆单词,再给定一些前缀用于查询,每次输入一个前缀,就输出单词中以这为前缀的单词数

题解:字典树基础

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;
char s[15];
struct Trie//字典树
{
    int cont;
    Trie *c[28];
    Trie()
    {cont=0;for(int i=0;i<=26;i++)c[i]=NULL;}
};
Trie *root;
void insert_into(char *str)//加入字典
{
    Trie *tmp=root;
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        int pos=str[i]-'a';
        if(tmp->c[pos]==NULL)
        {
            tmp->c[pos]=new Trie();
        }
        tmp=tmp->c[pos];
        tmp->cont++;
    }
}
int findTrie(char *str)//查找
{
    Trie *tmp=root;
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        int
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值