Pagini recente » Cod sursa (job #2405985) | Cod sursa (job #2796898) | Cod sursa (job #2560915) | Cod sursa (job #2590412) | Cod sursa (job #1787268)
#include<cstdio>
using namespace std;
FILE *fin = freopen("trie.in", "r", stdin);
FILE *fout = freopen("trie.out", "w", stdout);
const int SIGMA = 26;
int N, prefix;
char S[22];
struct Node
{
Node* son[SIGMA];
int nrAppear;
int nrPrefix;
};
Node* newEmptyNode()
{
Node* node = new Node;
for (int i = 0; i < SIGMA; i++)
node -> son[i] = NULL;
node -> nrAppear = 0;
node -> nrPrefix = 0;
return node;
}
void Insert(Node* node, char* word)
{
if (word[0] == 0)
node -> nrAppear ++;
else
{
if (node -> son[word[0] - 'a'] == NULL)
node -> son[word[0] - 'a'] = newEmptyNode();
node = node -> son[word[0] - 'a'];
node -> nrPrefix ++;
Insert(node, word + 1);
}
}
void Delete(Node* node, char* word)
{
if (word[0] == 0)
node -> nrAppear --;
else
{
if (node -> son[*word - 'a'] != NULL)
node = node -> son[*word - 'a'];
node -> nrPrefix --;
Delete(node, word + 1);
}
}
int NrAppear(Node* node, char* word)
{
if (*word == 0)
return node -> nrAppear;
if (node -> son[*word - 'a'] != NULL)
{
node = node -> son[*word - 'a'];
return NrAppear(node, word + 1);
}
return 0;
}
int Prefix(Node* node, char* word)
{
if (*word == 0)
return prefix;
if (node -> son[*word - 'a'] != NULL)
{
node = node -> son[*word - 'a'];
if (node -> nrPrefix != 0)
{
prefix ++;
return Prefix(node, word + 1);
}
}
return prefix;
}
int main()
{
Node *dictionary = newEmptyNode();
while (scanf("%d ", &N) && gets(S))
{
if (N == 0) Insert(dictionary, S);
else
if (N == 1) Delete(dictionary, S);
else
if (N == 2) printf("%d\n", NrAppear(dictionary, S));
else
{
prefix = 0;
printf("%d\n", Prefix(dictionary, S));
}
}
}