zoukankan      html  css  js  c++  java
  • poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001

    思路分析:

    在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目;

    在创建结点时,路径上的所有除叶子节点以外的结点的childNum增加1,叶子结点的childNum设置为1;

    在查询某个结点的最短前缀时:

    (1)若查找路径上的所有结点的childNum大于1,表明该字符串的最短路径为其自身;

    (2)若查找路径上存在结点的childNum等于1,表明查找的字符串是唯一以该字符串为前缀的字符串;

    代码如下:

    #include <iostream>
    #include <cstdlib>
    
    const int MAXN = 26;
    const int N = 1000 + 10;
    const int M = 20 + 1;
    char dic[N][M];
    struct Trie
    {
        int childNum;
        Trie *child[MAXN];
        Trie()
        {
            for (int i = 0; i < MAXN; ++ i)
                child[i] = NULL;
            childNum = 1;
        }
    };
    
    Trie *root = NULL;
    void insert(char *word)
    {
        Trie *cur = root;
        int len = strlen(word);
    
        for (int i = 0; i < len; ++ i)
        {
            int id = word[i] - 'a';
         
            if (cur->child[id] == NULL)
                cur->child[id] = new Trie;
            else
                cur->child[id]->childNum++;
            cur = cur->child[id];
        }
    }
    
    int findShortestPrefixes(char *word)
    {
        int i, len = strlen(word);
        Trie *cur = root;
    
        for (i = 0; i < len; ++ i)
        {
            int id = word[i] - 'a';
    
            cur = cur->child[id];
            if (cur->childNum <= 1)
                return i;
        }
        return -1;
    }
    
    int main()
    {
        int count = 0;
    
        root = new Trie;
        while (scanf("%s", dic[count]) != EOF)
            insert(dic[count++]);
    
        for (int j = 0; j < count; ++ j)
        {
            int ans = findShortestPrefixes(dic[j]);
    
            if (ans == -1)
                printf("%s %s
    ", dic[j], dic[j]);
            else
            {
                printf("%s ", dic[j]);
                dic[j][ans + 1] = '';
                printf("%s
    ", dic[j]);
            }
        }
    
        return 0;
    }
  • 相关阅读:
    android中kl布局文件加载失败解决办法
    android系统输入按键流程
    linux键值转android键值配置文件
    linux键值到Android键值的转换与自定义
    linux中ioctl的应用与说明
    zabbix邮件告警
    linux 双网关双IP设置
    随笔
    记录一次事故
    python解析.yml/.yaml文件--pyyaml模块(第三方)
  • 原文地址:https://www.cnblogs.com/tallisHe/p/4263042.html
Copyright © 2011-2022 走看看