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;
    }
  • 相关阅读:
    关于Oracle
    form表单中包含特殊字符,需要转义。
    mysql5.7解压版安装步骤
    mysql报1055错误
    配置maven私有仓库
    全选,反选
    前后端数据交互(json)
    正则表达式匹配html标签里的中文
    excel创建行、插入行、设置样式
    Python 中文字符的输出
  • 原文地址:https://www.cnblogs.com/kedebug/p/2872370.html
Copyright © 2011-2022 走看看