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;
    }
  • 相关阅读:
    SVN版本控制服务
    JVM内存结构
    Git的使用
    Nginx详解
    Apache(httpd)详解
    rsyslog日志收集器
    nsswitch名称解析框架
    NFS网络文件系统
    ThreadLocal详解
    RocketMQ踩坑记
  • 原文地址:https://www.cnblogs.com/kedebug/p/2873698.html
Copyright © 2011-2022 走看看