标签:
Description
Input
Output
Sample Input
Sample Output
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<cstdlib>
using namespace std;
struct Trie
{
int v;
Trie *next[26];
};
int rec;//用来记录每一次查询存在且以此为前缀的单词的个数
Trie root, *trie, *newtrie;
void Insert(char str[])
{
int len = strlen(str);
trie = &root;
for (int i = 0; i < len; i++)
{
int id = str[i] - ‘a‘;
if (trie -> next[id] == NULL)
{
newtrie = (Trie*)malloc(sizeof(Trie));
for (int i = 0; i < 26; i++) newtrie -> next[i] = NULL;
trie -> next[id] = newtrie;
trie -> next[id] -> v = 1;
trie = trie -> next[id];
}
else
{
trie -> next[id] -> v ++;
trie = trie -> next[id];
}
}
}
bool Search(char str[])
{
rec = 0;
int len = strlen(str);
trie = &root;
for (int i = 0; i < len; i++)
{
int id = str[i] - ‘a‘;
if (trie -> next[id] == NULL) return false;
else trie = trie -> next[id];
}
rec = trie -> v;
return true;
}
void DeleteAll(Trie *t)
{
for (int i = 0; i < 26; i++)
{
if (t -> next[i] != NULL)
{
Trie *t1 = t -> next[i];
DeleteAll(t1);
}
}
free(t);
}
void Delete(char str[])
{
if (!Search(str)) return ;