-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
63 lines (61 loc) · 1.36 KB
/
trie.cpp
File metadata and controls
63 lines (61 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
struct Node{
struct Node* filhos[26];
bool fim;
};
/*
Funcao que cria outro no com vetor de ponteiros filhos igual a null e fim igual a falso
*/
Node* getNode(){
Node* pNode=new Node;
pNode->fim=false;
for(int i=0;i<26;i++)
pNode->filhos[i]=NULL;
return pNode;
}
void insere(Node *root,string key){
Node* ptr=root;// ponteiro que aponta para o comeco
for (int i=0;i<key.length();i++){
int j=key[i]-'a';
if(ptr->filhos[j]==NULL){//se o filho for nulo
ptr->filhos[j]=getNode();//cria o ponteiro para o filho dele
}
ptr=ptr->filhos[j];//ponteiro vai para o filho
}
ptr->fim=true;//dizemos que e fim
}
bool procura(Node *root,string key){
Node *ptr=root;
for (int i=0;i<key.length();i++){
int j=key[i]-'a';
if(ptr->filhos[j]==NULL){// se ele nao tiver mais filhos e a string ainda nao tiver acabado
return false;
}
ptr=ptr->filhos[j];//ponteiro vai para o filho
}
if(ptr!=NULL && ptr->fim==true)return true;//se o filho nao for nulo e for fim retorna verdadeiro
return false;
}
int main()
{
int n;
scanf("%d", &n);
Node* root=getNode();
for(int i=1;i<=n;i++){
getchar();
string s;
cin >> s;
insere(root,s);
}
int m;
scanf("%d", &m);
for (int i=1;i<=m;i++){
getchar();
string s;
cin >> s;
if(procura(root,s))printf("YES\n");
else printf("NO\n");
}
return 0;
}