Pagini recente » Cod sursa (job #1039725) | Cod sursa (job #375076) | Cod sursa (job #891679) | Cod sursa (job #2870749) | Cod sursa (job #1415237)
#include <cstdio>
#include <cstring>
typedef struct _trie {
int numberOfWords;
int numberOfPasses;
_trie* nodes[26];
_trie() {
numberOfWords = 0;
numberOfPasses = 0;
for (int i = 0; i<26; i++)
{
nodes[i] = NULL;
}
}
}trie;
using namespace std;
int numberOfWordsGlobal;
void add(trie* root, char* s) {
if (*s == 0) {
root->numberOfWords += 1;
return;
}
if (root->nodes[s[0]-'a'] == NULL)
{
root->nodes[s[0] - 'a'] = new trie;
}
root->nodes[s[0] - 'a']->numberOfPasses += 1;
add(root->nodes[s[0] - 'a'],s+1);
}
void sterge(trie* root, char* s)
{
if (*s == NULL)
{
root->numberOfWords -= 1;
}
else
{
root->nodes[s[0] - 'a']->numberOfPasses -=1;
sterge(root->nodes[s[0] - 'a'],s+1);
}
}
int searchAparitie(trie* root, char*s)
{
if (*s == NULL)
{
return root->numberOfWords;
}
else if (root->nodes[s[0] -'a'] != NULL)
{
return searchAparitie(root->nodes[s[0] - 'a'],s+1);
}
else
{
return 0;
}
}
int getMaxNumber(trie* root,char* s)
{
int rez = 0;
trie t = *root;
for (int i = 0; i<strlen(s); i++)
{
if (t.nodes[s[i] - 'a'] != NULL && t.nodes[s[i]-'a']->numberOfPasses != 0)
{
rez += 1;
t = *(t.nodes[s[i]-'a']);
}
else
{
break;
}
}
return rez;
}
int main() {
int type;
char s[25];
trie* root;
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
root = new trie;
numberOfWordsGlobal = 0;
while (feof(stdin) == false)
{
if (scanf("%d %s",&type,s) == 2)
{
if (type == 0)
{
++numberOfWordsGlobal;
add(root,s);
}
else if (type == 1)
{
--numberOfWordsGlobal;
sterge(root,s);
}
else if (type == 2)
{
printf("%d\n",searchAparitie(root,s));
}
else
{
printf("%d\n",getMaxNumber(root,s));
}
}
}
return 0;
}