codevs——4189 字典&&HihoCoder #1014 : Trie树
最经,skyzhong得到了一本好厉害的字典,这个字典里整整有n个单词(1<=n<=200000)
现在skyzhong需要在字典里查询以某一段字母开头的单词
如:skyzhong想查询a
那么只要是a开头的单词就可以了
skyzhong只想知道里面有没有这一个单词(因为没有他就不查了)
若有,请输出YES。若没有,请输出NO
第一行一个数n
第二行到第n+1行,一行一个字符串
再下一行一个数m,表示skyzhong想要查询的次数
接着m行,一行一个字符串,表示skyzhong想要查的东西
共m行,若有这字串输出YES,否则输出NO
3
asd
asfdghj
asfd
3
asd
asdghj
asf
YES
NO
YES
字符串只有小写字母,且长度≤8
- #include<bits/stdc++.h>
- #define N 2000010
- using namespace std;
- //map<string ,int>W; //map淼
- int n,m,len,tot,trie[N][],sum[N],v[N];
- char s[];
- struct node{
- int count;
- node *next[];
- node(){count=,memset(next,,sizeof(next));}
- }*rt;
- void insert(){
- node *r=rt;
- char *word=s;
- while(*word){
- int id=*word-'a';
- if(r->next[id]==NULL) r->next[id]=new node;//r->next[id]=build();
- r=r->next[id];
- r->count++;
- word++;
- }
- }
- int search(){
- node *r=rt;
- char *word=s;
- while(*word){
- int id=*word-'a';
- r=r->next[id];
- if(r==NULL) return ;
- word++;
- }return r->count;
- }
- int main()
- {
- rt=new node;
- cin>>n;
- for(int i=;i<=n;i++){
- cin>>s;
- insert();
- }cin>>m;
- for(int i=;i<=m;i++){
- cin>>s;
- if(search()!=){
- cout<<"YES\n";
- }else cout<<"NO\n";
- }return ;
- }
2.数组模拟
- #include<bits/stdc++.h>
- #define N 2000010
- using namespace std;
- //map<string ,int>W; //map淼
- int n,m,len,tot,trie[N][],rt,sum[N],v[N];
- char s[];
- void insert(){
- len=strlen(s);
- rt=;
- for(int i=;s[i];i++){
- int id=s[i]-'a';
- if(!trie[rt][id]) trie[rt][id]=++tot;
- sum[trie[rt][id]]++;
- rt=trie[rt][id];
- }
- }
- int search(){
- len=strlen(s);
- rt=;
- for(int i=;s[i];i++){
- int id=s[i]-'a';
- if(!trie[rt][id]) return ;
- rt=trie[rt][id];
- }return sum[rt];
- }
- int main()
- {
- cin>>n;
- for(int i=;i<=n;i++){
- cin>>s;
- insert();
- }cin>>m;
- for(int i=;i<=m;i++){
- cin>>s;
- if(search()!=){
- cout<<"YES\n";
- }else cout<<"NO\n";
- }return ;
- }
3.Map水题版
- #include<bits/stdc++.h>
- using namespace std;
- map<string ,int>W;
- int n,m,len;
- char s[];
- int main()
- {
- cin>>n;
- for(int i=;i<=n;i++){
- cin>>s;
- len=strlen(s);
- for(int i=len;i>=;i--){
- s[i]='\0';
- W[s]++;
- }
- }cin>>m;
- for(int i=;i<=m;i++){
- cin>>s;
- if(W[s]!=){
- cout<<"YES\n";
- }else cout<<"NO\n";
- }return ;
- }
Trie树初探,参考博客:TRTTG
#1014 : Trie树
建树+查找
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #define N 10101010
- using namespace std;
- int n,m,tot,tri[N][],vi[N];
- string s;
- void insert(){
- int l=s.length(),root=;
- for(int i=;i<l;i++){
- int id=s[i]-'a';
- if(!tri[root][id]) tri[root][id]=++tot;
- root=tri[root][id];
- vi[root]++;
- }
- }
- int ask_(){
- int l=s.length(),root=;
- for(int i=;i<l;i++){
- int id=s[i]-'a';
- if(!tri[root][id]) return ;
- root=tri[root][id];
- }
- return vi[root];
- }
- int main()
- {
- scanf("%d",&n);
- for(int i=;i<=n;i++) {
- cin>>s;
- insert();
- }
- scanf("%d",&m);
- for(int i=;i<=m;i++){
- cin>>s;
- printf("%d\n",ask_());
- }
- return ;
- }
codevs——4189 字典&&HihoCoder #1014 : Trie树的更多相关文章
- Hihocoder #1014 : Trie树 (字典数树统计前缀的出现次数 *【模板】 基于指针结构体实现 )
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...
- hihoCoder #1014 : Trie树 [ Trie ]
传送门 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...
- hihoCoder 1014 Trie树 (Trie)
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...
- hihoCoder 1014 : Trie树(字典树)
传送门 Description 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小 ...
- hihoCoder#1014 Trie树 (前缀树)
题目大意:给一本有n个单词的词典,有m次询问,每次询问的是该词典中有多少个单词有共同的某个前缀. 题目分析:在添加单词建立trie的时候,每经过一个节点就意味着该节点和它的各级祖先节点是某个单词的前缀 ...
- hihocoder 1014: Trie树(Trie树模板题)
题目链接 #include<bits/stdc++.h> using namespace std; ; struct T { int num; T* next[]; T() { num=; ...
- hiho #1014 : Trie树
#1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助, ...
- Codevs 4189 字典(字典树Trie)
4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 传送门 题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个字典里 ...
- #1014 : Trie树 HihoCoder(字典树)
描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题: ...
随机推荐
- QSettings读写注冊表、配置文件
简述 普通情况下.我们在开发软件过程中,都会缓存一些信息到本地,能够使用轻量级数据库sqlite.也能够操作注冊表.读写配置文件. 关于QSettings的使用前面已经介绍过了.比較具体,见" ...
- 虚拟化(二):虚拟化及vmware workstation产品使用
虚拟化(一):虚拟化及vmware产品介绍 vmware workstation的最新版本号是10.0.2. 相信大家也都使用过,当中的简单的虚拟机的创建.删除等,都非常easy.这里就不再具体说明了 ...
- LeetCode 463. Island Perimeter (岛的周长)
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- linux下非root用户怎样改动root权限的文件
在linux下会出现把一些配置文件參数配错.rootpassword忘记等导致系统无法启动或进入root的窘迫境界.本文以redhat enterprise linux server ...
- 软件project—思考项目开发那些事(一)
阅读文件夹: 1.背景 2.项目管理,质量.度量.进度 3.软件开发是一种设计活动而不是建筑活动 4.高速开发(简单的系统结构与复杂的业务模型) 5.技术人员的业务理解与产品经理的业务理解的终于业务模 ...
- 1250 Fibonacci数列
1250 Fibonacci数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 定义:f ...
- tiny4412 裸机程序 九、串口排查驱动原因及字符图片显示【转】
本文转载自:http://blog.csdn.net/eshing/article/details/37410571 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 一 ...
- ssh连接超时问题解决方案,每一种方案都可以
1.服务端修改 vim /etc/ssh/sshd_config 修改 ClientAliveInterval 60 ClientAliveCountMax 40 60秒,向客户端发送一次请求. 超过 ...
- canvas做的一个写字板
<!DOCTYPE html><html><head><title>画板实验</title> <meta charset=" ...
- 设计模式 |备忘录模式(memento)
定义: 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态. 结构:(书中图,侵删) Originator:需要备份的类(写在便签上 ...