惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
Latest news
Latest news
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
V
V2EX
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
小众软件
小众软件
A
Arctic Wolf
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
G
GRAHAM CLULEY
罗磊的独立博客
T
Tor Project blog
C
Cisco Blogs
美团技术团队
博客园 - Franky
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
Cyberwarzone
Cyberwarzone
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Security Latest
Security Latest
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
Spread Privacy
Spread Privacy
J
Java Code Geeks
C
CERT Recently Published Vulnerability Notes
大猫的无限游戏
大猫的无限游戏
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Project Zero
Project Zero

Lss233's.Blog()

10 分钟快速入门垃圾回收机制 2022: 艰难,但充满希望 MiniDB 开发手札2 - 网络通信: PostgreSQL 服务端实现 MiniDB 开发手札1 - 概览 HELLO: 2022 OpenVINO + YoloV5 目标视觉炼丹流程简述 使用 Tinc 组建虚拟内网,并接入 DN42 TOJ 1175 - 线段树模板题 HDU 1671 - Phone List 于是乎,我就这样活过了2020:这是一篇年末总结 这个Lss233一事无成却敢写年末总结:2019,再见啦。 噩梦24小时:记一次服务器迁移与宕机过程 新技能学习:教你如何阅读Java字节码 让我们用PGP进行安全地交流吧!
HDU 1257 - 统计难题
Lss233 · 2021-01-07 · via Lss233's.Blog()

Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).

输入

输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.

输出

对于每个提问,给出以该字符串为前缀的单词的数量.

样例输入

banana
band
bee
absolute
acm

ba
b
band
abc

样例输出

2
3
1
0

题解

所以,所谓的字典树就是把一个字符串都存进一棵树,每个节点都放一个字符。所以,对于每个节点至多会有 26个子节点。
然后我们可以在节点上存一点别的玩意,来满足题目想要的效果。
比如说, end 表示这是最后一个字符,那么从根节点到这个节点所经过的字符就正好构成了这个
词。
count 表示这个字符在同样的位置出现了多少次。
etc.

代码

#include<bits/stdc++.h>
#include <stdio.h>
struct node {
    bool end;
    node * children[26];
    int count = 0;
    ~node() {
        for (int i = 25; i >= 0; i--)
        {
            if(this->children[i] != NULL) {
                this->children[i]->~node();
                delete this->children[i];
            }
        }
    }
    node() {
        end = false;
        for(int i = 0; i < 26; i++)
            children[i] = NULL;
    }
};
node * root;
/**
 * 插入一个新的字符串
 * @param str 字符串数组
 * @param len 字符串长度
 **/
void insert(char* str, int len) {
    if(!root) {
        root = new node;
    }
    node *location = root;
    for(int i = 0; i < len; i++) {
        if(str[i] == 0 || str[i] == '\n') continue;
        int id = str[i] - 'a';
        if(location->children[id] == NULL) {
            location->children[id] = new node;
        }
        location = location->children[id];
        location->count++;
    }
    location->end = true;
}
/**
 * 查找指定字符串在树中的长度
 * @param str 字符串数组
 * @param len 字符串长度
 **/
int search(char * str, int len) {
    node * location = root;
    for (int i = 0; i < len; i++)
    {
        int id = str[i] - 'a';
        if(location->children[id] == NULL)
            return 0;
        location = location->children[id];
    }
    return location->count;
}
char str[20];
int main() {
    while (1)
    {
        fgets(str, 15, stdin);
        int len = strlen(str);
        if(len == 1) {
            break;
        }
        insert(str, len);
    }
    while (~scanf("%s", str))
    {
        int len = strlen(str);
        if(len == 0) {
            break;
        }
        printf("%d\n", search(str, len));
    }
    
}