Codevs4189 字典
来源:互联网 发布:水滴桌面软件 编辑:程序博客网 时间:2024/06/08 15:50
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int trie[1600001][26],len,root,tot,sum[1600001]; char s[11];//这里的s设为全局变量,插入和查询都是用的这个s void insert(){ len=strlen(s); root=0; for(int i=0;i<len;i++) { int id=s[i]-'a'; if(!trie[root][id]) trie[root][id]=++tot; //如果root子节点不含id所表示的字母,那么给trie[root][id]编号 //sum[trie[root][id]]++;//sum表示以trie[root][id]为前缀的单词出现的的次数 root=trie[root][id];//更改根节点,继续向下 }}bool search(){ root=0; len=strlen(s);//此时的s为要查询的前缀 for(int i=0;i<len;i++) { int id=s[i]-'a'; if(!trie[root][id]) return 0; root=trie[root][id]; } return 1;}int main(){int n,m;scanf("%d",&n);while(n--){cin>>s;insert();}scanf("%d",&m);while(m--){cin>>s;if(search()) puts("YES\n");else puts("NO\n");}}
阅读全文
0 0
- Codevs4189 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 字典
- 5.0 引用类型
- UVA
- POj 2235 Wireless Network
- 算法爱好者——颜色分类 ? 待解决
- 关于tomcat启动日志的选择性打印和禁止打印的问题
- Codevs4189 字典
- php中fastcgi和php-fpm
- 函数(七)
- 【人工智能】第四章 超越经典搜索
- Android 7.0 报android.os.FileUriExposedException异常
- 欢迎使用CSDN-markdown编辑器
- 微服务实战-端到端流程
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- OAuth2.0 的理解