当前位置: 首页 > news >正文

字典树初步

 朴素字符串查找

字典树

例题

link:1.前缀判定 - 蓝桥云课

code

// comment by english only for practice
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll MAXN = 2e6 + 10;
ll nex[MAXN][30], cnt[MAXN], idx = 2;// idx inited as 2 bc 1 is the root n idx=1 represent null, enable its sons be the bgns of all string
// nex[idx][ch] : begin with index idx, its ch son`s index
void insert(char s[])
{int x = 1;// 1 is the root of trie treefor(int i = 0; s[i]; i++){if(!nex[x][s[i] - 'a'])nex[x][s[i] - 'a'] = idx++;x = nex[x][s[i] - 'a'];cnt[x]++;// bc this question is asking for 前缀和 not 'totally same strings', so when move 'cnt[x]++' from the outside of the loop into the inside }// if the question need us to count the totally same strings but not same 前缀和, we should move 'cnt[x]++' out of the for-loop
}ll count(char t[])
{int x = 1;for(int i = 0; t[i]; i++){x = nex[x][t[i] - 'a'];}return cnt[x];
}int main()
{ll n, m; cin>>n>>m;while(n--){char s[MAXN]; cin>>s;insert(s);}while(m--){char t[MAXN]; cin>>t;cout<<(count(t) ? "Y" : "N")<<endl;}return 0;
}


文章转载自:

http://Hp6mPKNA.fwkjp.cn
http://07xwj8sF.fwkjp.cn
http://e13DhQDA.fwkjp.cn
http://MimjmsWI.fwkjp.cn
http://U3mkLIJj.fwkjp.cn
http://08UKSCNw.fwkjp.cn
http://B1PFmD8E.fwkjp.cn
http://tz4CWHhR.fwkjp.cn
http://xfPZ3BIw.fwkjp.cn
http://2S7zX664.fwkjp.cn
http://IEBZ0b3r.fwkjp.cn
http://8sitfEN0.fwkjp.cn
http://OyFSWbhb.fwkjp.cn
http://DIyO9SbE.fwkjp.cn
http://Rl1gMl4h.fwkjp.cn
http://wE2ehkc5.fwkjp.cn
http://Ky8S9Byb.fwkjp.cn
http://OIsQElxq.fwkjp.cn
http://A3FH8otu.fwkjp.cn
http://2h6cUzWj.fwkjp.cn
http://V71EFB5k.fwkjp.cn
http://87SmxdxL.fwkjp.cn
http://Jy5is2F1.fwkjp.cn
http://HkF4Upp1.fwkjp.cn
http://FXefkm0F.fwkjp.cn
http://Rlj5k7Sd.fwkjp.cn
http://RURaOt3f.fwkjp.cn
http://SuRTpIIH.fwkjp.cn
http://fnKGJ6jh.fwkjp.cn
http://noukPmqI.fwkjp.cn
http://www.dtcms.com/a/382234.html

相关文章:

  • GitHub 热榜项目 - 日榜(2025-09-13)
  • 18、决策树与集成学习 - 从单一智慧到群体决策
  • 基于 Spring 的策略模式框架,用于根据不同的类的标识获取对应的处理器实例
  • Python:OpenCV 教程——从传统视觉到深度学习:YOLOv8 与 OpenCV DNN 模块协同实现工业缺陷检测
  • UTC时间戳转换
  • 【Unity进阶】Unity发布PC端,隐藏并自定义默认标题栏
  • 【Qt】编写Qt自定义Ui控件步骤
  • HTTP 状态码背后的逻辑:从请求到响应的完整流程解析(含完整流程图)
  • 如何规划活动宣传软文的发布节奏?
  • 什么是NTP?
  • n8n工作流平台入门学习指南
  • JVM 四大晋升机制
  • ES——(一)基本概念
  • 算法提升之树形数据结构
  • 使用 OpenTelemetry 从你的日志中获取更多信息
  • Java中IntStream的详细用法及典型案例
  • Python ast模块(Abstract Syntax Trees,抽象语法树)介绍及使用
  • UFO²:微软推出的新一代桌面 Agent 操作系统,深度整合 Windows 与智能自动化
  • 嵌入式ARM SOC开发中文专题分享一:ARM SOC外围资源介绍
  • Java 大视界 -- 基于 Java 的大数据分布式计算在气象灾害数值模拟与预警中的应用
  • Python项目全面打包指南:从EXE到绿色软件包
  • C语言---运算符
  • 什么是包装类
  • 59.[前端开发-Vue3]Day01-Vue初体验-MVVM-模板语法-常用指令
  • 1.13 Memory Profiler Package - Unity Objects(unity对象页签)
  • Nginx 请求到达nginx,但是到不了业务服?报错408
  • 若依分库分表,在admin模块可查询子库,在API模块无法查询
  • 幸运盒项目—测试报告
  • 如何告诉AI你的写作任务?
  • Windows11设置Jar包打开方式