huu 1251
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <utility> 7 #include <vector> 8 #include <map> 9 #include <queue> 10 #include <stack> 11 #include <cstdlib> 12 #include <deque> 13 #include <set> 14 typedef long long ll; 15 #define lowbit(x) (x&(-x)) 16 #define ls l,m,rt<<1 17 #define rs m+1,r,rt<<1|1 18 using namespace std; 19 const int N=1e6+3; 20 int tree[N][30];//26个字母 21 int num[N]; 22 char s[15]; 23 int pos; 24 void insert(char s[]){ 25 int root=0,i; 26 for(i=0;s[i];i++){ 27 int x=s[i]-'a'; 28 if(!tree[root][x]){ 29 tree[root][x]=pos++; 30 } 31 root=tree[root][x]; 32 num[root]++; 33 } 34 } 35 int query(char s[]){ 36 int i; 37 int root=0; 38 for(i=0;s[i];i++){ 39 int x=s[i]-'a'; 40 if(!tree[root][x]){ 41 return 0; 42 } 43 root=tree[root][x]; 44 } 45 return num[root]; 46 } 47 int main() 48 { 49 pos=1; 50 /* 51 memset(tree,0,sizeof(tree)); 52 memset(num,0,sizeof(num)); 53 */ 54 //加上上面的会超内存 55 while(gets(s)){//不要用scanf 56 if(s[0]==NULL) 57 break; 58 insert(s); 59 } 60 while(gets(s)){ 61 printf("%d\n",query(s)); 62 } 63 return 0; 64 }
分类:
字典树
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现