整理一下自己的字典树模板。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 15;
typedef struct Trie_Node
{
struct Trie_Node *next[26];
int val;
}Trie;
void insert(Trie *root, char *s)
{
Trie *p = root;
int i = 0;
while(s[i] != '\0')
{
if(p->next[s[i]-'a'] == NULL)
{
Trie *temp = new Trie;
for(int j=0;j<26;j++)
temp->next[j] = NULL;
temp->val = 0;
p->next[s[i]-'a'] = temp;
}
p = p->next[s[i]-'a'];
p->val++;
i++;
}
}
int Find(Trie *root, char *s)
{
Trie *p = root;
int i = 0;
while(s[i] != '\0')
{
if(p->next[s[i]-'a'] == NULL)
return 0;
p = p->next[s[i]-'a'];
i++;
}
return p->val;
}
int main()
{
char s[20];
Trie *root = new Trie;
for(int i=0;i<26;i++)
root->next[i] = NULL;
root->val = 0;
while(gets(s) && strlen(s)) insert(root, s);
while(gets(s))
{
printf("%d\n", Find(root,s));
}
return 0;
}