F06【模板】字典树(Trie)

视频链接:F06 字典树(Trie)_哔哩哔哩_bilibili

 

 

 

Luogu P8306【模板】字典树

复制代码
// O(n)
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int N=100010; int n; char s[N]; int ch[N][26],cnt[N],idx; void insert(char *s){ int p=0; for(int i=0; s[i]; i ++){ int j=s[i]-'a';//字母映射 if(!ch[p][j])ch[p][j]=++idx; p=ch[p][j]; } cnt[p]++;//插入次数 } int query(char *s){ int p=0; for(int i=0; s[i]; i ++){ int j=s[i]-'a'; if(!ch[p][j]) return 0; p=ch[p][j]; } return cnt[p]; } int main(){ scanf("%d",&n); while(n--){ char op; scanf("%s%s",&op,s); if(op=='I')insert(s); else printf("%d\n",query(s)); } return 0; }
复制代码

 

练习:

Luogu P4407 [JSOI2009] 电子字典

Luogu P6088 [JSOI2015] 字符串树

Luogu [AGC057C] Increment or Xor

Luogu P6623 [省选联考 2020 A 卷] 树

 

posted @   董晓  阅读(1486)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示