hdu 1251 统计难题 字典树

字典树模板 hdu1251统计难题 统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 41407    Accepted Submission(s): 14930 Problem Description Ignatius最近遇到一个难题,老 阅读详情

在hdu上要用c++提交,要不然会超内存。

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1251

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define maxn 30

struct Trie{
    Trie * next[maxn];
    int v;
};

Trie * root;

void Init_Trie(){
    root = (Trie *)malloc(sizeof(Trie));
    for(int i=0;i<maxn;i++){
        root -> next[i] = NULL;
    }
}

void Add_Trie(char * str){
    Trie * p = root , * q;
    for(int i=0;str[i]!='\0';i++){
        int id = str[i] - 'a';
        if(p->next[id]==NULL){
            q = (Trie *)malloc(sizeof(Trie));
            q -> v = 1;
            for(int j=0;j<maxn;j++){
                q -> next[j] = NULL;
            }
            p -> next[id] = q;
            p = p -> next[id];
        }
        else {
            p -> next[id]->v++;
            p = p -> next[id];
        }
    }
    //p -> v ++;
}

int Search_Trie(char * str){
    Trie * p = root;
    for(int i=0;str[i]!='\0';i++){
        int id = str[i] - 'a';
        p = p -> next[id];
        if(p==NULL){
            return 0;
        }
    }
    return p -> v;
}

void Del_Trie(Trie * p){
    for(int i=0;i<maxn;i++){
        if(p->next[i])Del_Trie(p->next[i]);
    }
    free(p);
}

void input(){
    char a[15];
    Init_Trie();
    while(gets(a),a[0]!='\0'){
        Add_Trie(a);
    }
    while(~scanf("%s",a)){
        printf("%d\n",Search_Trie(a));
    }
    Del_Trie(root);
}

void File(){
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
}

int main(void){
    //File();
    input();
    return 0;
}



[字典树 ]hdu 1251 统计难题 /** [字典树 ]hdu 1251 统计难题 统计前缀出现次数 */ #include #include #define N 1000000 struct tireTree { int cnt; tireTree *child[26]; }tt[N],*spt; void insert(char *s,tireTree *&rt) { tireTree *loc; 阅读详情

相关推荐

PSOC Creator 入门教程超详细

最近工作原因需要使用到赛普拉斯的一款芯片,奈何以前没有用过,所以趁这次也学习了一些简单的用法,总结一下,希望能帮到有需要朋友,因为网上关于这方面的文章少的可怜,详细点的更是少之又少!废话少说,正文开始.

qm_mq的博客 5757

HDU 1251 统计难题 字典树Trie模板

HDU 1251 统计难题 这是一道字典树Trie模板题,记录下来以便以后复习时使用。 Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). [输入] 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文

weixin_46283740的博客 377

Loran-C(罗兰C)信号捕获算法及MATLAB仿真代码

本文首先介绍了Loran-C信号的时域波形及编码方式,然后描述了信号的捕获及相位匹配原理,包括相关运算和并行码相位搜索,最后给出信号及捕获算法仿真及结果。

BoilingHotPot的博客 2662

HDU - 1251 统计难题 (字典树)

Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只...

dsaghjkye的博客 266

ROS2实战:构建模块化启动文件(launch file)以驱动复杂机器人系统

本文详细介绍了ROS2中构建模块化启动文件(launch file)的最佳实践,帮助开发者高效管理复杂机器人系统的节点启动。通过功能分解、参数化设计和嵌套启动等技术,实现批量管理、依赖控制和配置统一,显著提升系统稳定性和调试效率。文章包含实战代码示例和工程化建议,特别适合自动驾驶、物流机器人等场景应用。

weixin_28744613的博客 218

Unity工业数据可视化:基于ModbusTCP的实时数据通信实战

本文详细介绍了在Unity中实现工业数据可视化的核心方法,重点讲解了基于ModbusTCP协议与PLC等工业设备进行实时数据通信的实战流程。内容涵盖NModbus库的集成、Modbus Slave模拟器的使用、TCP连接的建立与多线程安全通信、数据读写与解析,以及如何将实时数据驱动3D场景,为构建工业数字孪生与监控系统提供完整解决方案。

ol7890123的博客 558

hdu 1251 统计难题(字典树)

题目链接:hdu 1251 统计难题 题目大意:略。 解题思路:字典树水题,插入单个单词的时候,对路径上的节点+1,查询时返回最后落到节点的计数。 #include #include #include using namespace std; const int maxn = 1000005; const int sigma_size = 26; struct Tire

不慌不忙、不急不躁 721

ubuntu安装英伟达显卡驱动后wifi消失解决方法

ubuntu安装英伟达显卡驱动后(比如4090和3090等),会把wifi和蓝牙驱动去除,导致wifi无法连接。需要重新下载驱动安装。

人工智能(AI)技术,大模型技术,深度学习,机器学习,计算机视觉,AI工具实践应用等分享 3911

hdu 1251 统计难题 字典树

传送门:hdu1251统计难题 中文题目不做过多的解释解题思路已经接近于字典树的模板题目,每次把字典中的单词加入到树里面。 先是要创建这棵树, 1. 线定义结构体,先看我们定义的结构体typedef struct Trie{ int v; Trie *next[26]; }Trie; 创建字典树函数,字典树的创建如下图:, 这个图对应的过程就是, 首先是一个根节点,他是所有

woshinannan的专栏 462

【Bug已解决】NCCL timeout for validation 解决方案

训练用多卡 DDP / FSDP 正常,但一到或者进程直接 hang 在 eval 的某个集合通信上。最迷惑的是:训练阶段(有集合通信)完全正常,偏偏 eval 阶段(看起来更轻量)超时。因为 eval 阶段某些 rank 没参与 collective,导致参与的那部分 rank 永远等不到同伴。

一键难忘的博客 148

BES2810_best1702_best1702_ibrt_三者关系深度解析

一颗芯片的三个名字:BES2810、best1702 与 best1702_ibrt 关系深度解析

weixin_42162924的博客 359

基于Spring Boot的实验室安全考试系统设计与部署实践

在高校和企业实验室管理中,安全考试是保障人员安全的重要环节。传统纸质考试在出题、监考、批改等环节效率低下,数字化考试系统成为提升考核效率与可信度的有效手段。Spring Boot作为Java生态中主流的开发框架,凭借自动装配、内置容器和快速部署等特性,为构建中小型业务系统提供了稳定高效的技术基础。本文从需求分析、技术选型、核心模块设计到打包部署,系统阐述如何基于Spring Boot实现实验室安全考试系统,涵盖题库管理、随机组卷、在线答题、自动判分、成绩统计等关键功能,并总结开发过程中遇到的典型问题与解决思

weixin_34284188的博客 338

C# WPF上位机实战:从零开发空压机监控系统(含实时监控+故障预警+数据追溯)

在工业生产场景中,空压机作为核心动力设备,常面临这些痛点:压力/温度异常导致停机、油耗超标未及时发现、设备故障追溯无数据支撑、人工巡检效率低且易遗漏。而基于C# WPF开发的空压机监控系统,能通过实时采集设备运行参数、智能分析运行状态、联动故障预警,实现“状态监控-异常告警-数据追溯-远程管控”全流程自动化,完美解决传统管理模式的弊端。今天就带大家落地这套空压机监控系统,基于C# + .NET 8 + WPF + Modbus TCP/RTU协议,整合压力、温度、油耗、运行时长等核心参数监控,搭配WPF的数

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1111

HDU1251——统计难题【字典树】

HDU1251——统计难题 大致思路:我们可以让字典树的节点记录每一个节点的出现的次数,然后在我们查找的过程中直接查找最后一个字符对应的节点的次数即可。 #include &lt;algorithm&gt; #include &lt;cstdio&gt; #include &lt;cstring&gt; #include &lt;iostream&gt; using namespace s...

xiang_hehe的博客 254

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

hdu 1251 统计难题 (字典树入门题)

1 /******************************************************* 2 题目: 统计难题 (hdu 1251) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 4 算法: 字典树 5 提示: 这题压要用c++提交,G++会超内存 6 **********...

weixin_30622107的博客 110

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

hdu1251 统计难题 字典树

hdu 1251 统计难题   字典树的解释及模板   背下来..     #include using namespace std; struct trie { int num; bool isword; trie *child[26]; trie() { for(int i=0;i<26;i++) child[i]=0; num=0;isword=0; } }

Time flies 924

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

HDU 1251 统计难题 【字典树】

Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连

Joeferyの博客 496

hdu 1251 统计难题 (字典树)

题目链接:   hdu 1251 题目大意:   给出单词的词典,然后有限次询问                   每次询问给出的字符在词典中作为前缀的次数 解题思路:   建立词典的字典树                   用w标记此结点在建树过程中访问的次数,每经过一次就+1                   查询时把查询的字符遍历字典树,遍历最后结点的w值既是答案

ンZee的解题报告 1315

HDU1251 统计难题【字典树】

题目大意: 给你一张单词表,每个单词占一行,以空行结束。再给你几个单词前缀。那么问题来了: 统计出单词表中以所给单词前缀为前缀的单词数目。 思路: 其实就是字典树的模板应用。根据所给单词表建立一个字典树,并记录所有前缀的个数。 然后根据所给单词前缀去字典树中查找是否含有这个前缀。找到就输出该前缀的个数。

程序员充电站(itcharge) 1200

HDU 1251 统计难题 字典树大水题

今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧。 HDU-1251统计难题 这道题唯一的问题就是会不会字典树, 2333, 给一个字典树的博客传送门, 话说这个博客一搜就搜到了啊. 代码: 1 #include<bits/stdc++.h> 2...

dengpangbu3703的博客 117

统计难题 【 HDU - 1251 】【 字典树 】

 统计难题  HDU - 1251  简单字典树。 #include &lt;iostream&gt; #include &lt;string.h&gt; using namespace std; struct node { int num; struct node *next[26]; node(){ for(int i = 0; i &lt;...

Mercury_Lc的博客 410

HDU1251-统计难题 【字典树】

Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注...

独钓门前月 414

HDU 1251 统计难题(字典树模板)

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 38305    Accepted Submission(s): 14058 Problem Description Ignatius最近遇到一个难题,老

Usher_Ou的博客 483

HDU 1251统计难题(字典树)

HDU 1251统计难题(字典树) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 52010 Accepted Submission(s): 18240 Problem Description Ignatius最近遇到一个难...

博学 笃志 切问 近思 306

【字典树】HDU1251统计难题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 用的是指针,指针不懂的就得好好先稍微懂一点指针在来轻松搞定; 这个博客写的还不错,可以推荐看一下。链接:http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html #include #include #include #

wlxsq的专栏 1200

HDU 1251 统计难题(字典树模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 HDU 1251 统计难题题意思路代码 题意         统计出以某个单词作为前缀的单词的数量,单词本身也属于单词的前缀。如字符串abc的前缀为a,ab,abc。 思路         字典树模板题目,在往字典树内插入一个单词时,每个经过的结点

Chstelove的博客 258
上一篇: hdu 1671 Phone List 字典树
下一篇: hdu 1075 What Are You Talking About 字典树
_NMfloat_
博客等级 码龄12年 7粉丝 315原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址