zoukankan      html  css  js  c++  java
  • HDOJ 2072 单词数(trie树入门)

    思路:

    虽然用 set 一下子就做出来了,但还是坚持用字典树实现了一遍。

    #include <iostream>
    using namespace std;
    
    struct node {
        bool isword;
        int child[26];
    } trie[10010] ;
    
    int size = 0;
    int count = 0;
    
    void Insert(char* word, int i, int rt)
    {
        if (!word[i])
        {
            if (!trie[rt].isword)
                ++count, trie[rt].isword = true;
            return ;
        }
    
        int m = word[i] - 'a';
        if (trie[rt].child[m])
        {
            Insert(word, i + 1, trie[rt].child[m]);
        }
        else
        {
            trie[rt].child[m] = ++size;
            Insert(word, i + 1, size);
        }
    }
    
    int main()
    {
        char str[2000];
        while (gets(str) && str[0] != '#')
        {
            count = 0;
            size = 0;
            memset(trie, 0, sizeof(trie));
    
            char word[100];
            int j = 0;
            memset(word, 0, sizeof(word));
            for (int i = 0; str[i] != '\0'; ++i)
            {
                if (str[i] != ' ')
                    word[j++] = str[i];
                else if (j)
                {
                    Insert(word, 0, 0);
                    memset(word, 0, sizeof(word));
                    j = 0;
                }
            }
            if (j)
                Insert(word, 0, 0);
    
            printf("%d\n", count);
        }
        return 0;
    }
  • 相关阅读:
    apply()和call()的区别
    强制类型转换
    浮动理解
    清除浮动的方式
    五大主流浏览器及四大内核
    CSS引入方式
    js构建类的方法
    web前端与后端的理解区分
    Java的API及Object
    面向对象之this关键字
  • 原文地址:https://www.cnblogs.com/kedebug/p/2873698.html
Copyright © 2011-2022 走看看