Trie字典树

 #include "stdio.h"
#include "iostream"
#include "malloc.h"
#include "string.h" using namespace std; #define MAX_SIZE 26 typedef struct Trie{
char val;
bool isword;
struct Trie* child[MAX_SIZE];
}Node,*Trie_pointer; Trie_pointer CreateNode()
{
Trie_pointer node;
node = (Trie_pointer)malloc(sizeof(Node));
memset(node,,sizeof());
return node;
} void Insert(Trie_pointer root, char *s)
{
Trie_pointer tmp,t = root;
char *p = s;
if(*s == '\0')
return;
while(*p != '\0')
{
if(t->child[*p - 'a'] == NULL)
{
tmp = CreateNode();
tmp->val = *p;
t->child[*p - 'a'] = tmp;
}
t = t->child[*p - 'a'];
p++;
}
t->isword = ;
} bool Search(Node root, char *s)
{
Trie_pointer t = &root;
char *p = s;
if(*s == '\0')
return false;
while(*p != '\0')
{
if(t->child[*p - 'a'] == NULL)
return false;
t = t->child[*p - 'a'];
p++;
}
if(t->isword == )
return false;
return true;
} int main()
{
Node root;
char s[];
int i,n;
memset(&root,,sizeof(Node));
cin>>n;
for(i=; i<=n; i++)
{
cin>>s;
Insert(&root,s);
}
while(cin>>s)
{
int flag = Search(root,s);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
05-11 01:13