zoukankan      html  css  js  c++  java
  • HDOJ 1305 Immediate Decodability(trie树入门)

    题意:

    即判断某一个字符串是否为其他字符串的前缀。

    思路:

    字典树。每个单词增加一个标记,表示这个字典是否是一个单词。

    下次输入的时候就可以提前判断了。

    #include <iostream>
    using namespace std;
    
    struct node {
        bool isword;
        int child[2];
    } trie[1100] ;
    
    int size = 0;
    bool flag = false;
    
    void Insert(char* word, int i, int rt)
    {
        if (!word[i])
        {
            trie[rt].isword = true;
            return ;
        }
    
        int m = word[i] - '0';
        if (trie[rt].child[m])
        {
            if (trie[trie[rt].child[m]].isword || !word[i+1])
                flag = true;
            Insert(word, i + 1, trie[rt].child[m]);
        }
        else
        {
            trie[rt].child[m] = ++size;
            Insert(word, i + 1, size);
        }
    }
    
    int main()
    {
        char word[12];
        int cases = 0;
        while (scanf("%s", word) != EOF)
        {
            if (word[0] == '9')
            {
                if (flag)
                    printf("Set %d is not immediately decodable\n", ++cases);
                else
                    printf("Set %d is immediately decodable\n", ++cases);
                size = 0;
                flag = false;
                memset(trie, 0, sizeof(trie));
                continue;
            }
            Insert(word, 0, 0);
        }
        return 0;
    }
  • 相关阅读:
    允许debian wheezy支持IOS7+的iphone.
    openSUSE 国内镜像摘要
    策略模式总结
    顺序串
    WindowState注意事项
    NLP | 自然语言处理
    Using Autorelease Pool Blocks
    NSAutoreleasePool & thread
    oc语言特性
    oc语言基础整理
  • 原文地址:https://www.cnblogs.com/kedebug/p/2872370.html
Copyright © 2011-2022 走看看