zoukankan      html  css  js  c++  java
  • hiho一下

    #include <iostream>
    #include <stdio.h>
    using namespace std;
    struct Tire{
        int num;
        Tire* next[26];
        Tire()
        {
            num=0;
            int i;
            for(i=0;i<26;i++)
                next[i] = NULL;
        }
    }tree;
    
    void Insert(char word[])
    {
        Tire* p = &tree;
        int i;
        for(i=0;word[i];i++){
            int t = word[i]-'a';
            if(p->next[t]==NULL)
                p->next[t] = new Tire;
            p = p->next[t];
            p->num++;
        }
    }
    
    int Find(char word[])
    {
        Tire* p = &tree;
        int i;
        for(i=0;word[i];i++){
            int t = word[i]-'a';
            if(p->next[t]==NULL)
                return 0;
            p = p->next[t];
        }
        return p->num;
    }
    
    int main()
    {
        int n;
        char word[11];
        //字典
        scanf("%d",&n);
        gets(word);
        while(n--){    //读取单词n次
            gets(word);    
            Insert(word);    //将单词插入到字典树中
        }
        //询问
        scanf("%d",&n);
        gets(word);    //将回车读入
        while(n--){
            gets(word);
            printf("%d
    ",Find(word));    //查询以word为前缀的单词出现过几次
        }
        return 0;
    }

    是时候学一下Trie了。挺简单的。其实字母树就是一棵多维的线段树。用来统计。

    当然zkw那么牛逼我比不了,只能随便粘粘代码

  • 相关阅读:
    Objective-C 复合
    useContext的使用
    context的使用
    redux使用(二)
    redux使用(一)
    React class & function component 的区别
    combineReducers使用
    gnvm使用(未使用成功)
    React相关知识点
    eslint简单使用&&eslint与webpack结合使用
  • 原文地址:https://www.cnblogs.com/Rainb/p/3847987.html
Copyright © 2011-2022 走看看