Pagini recente » Cod sursa (job #1845812) | Cod sursa (job #1944409) | Cod sursa (job #1628712) | Cod sursa (job #3147171) | Cod sursa (job #2453994)
#include <bits/stdc++.h>
#define lmax 25
using namespace std;
class trie
{
private:
#define uint unsigned int
#define endWord(w) (!(*w))
static const uint alphabetSize = 26;
class trieNode
{
private:
int cnt = 0, oth = 0;
trieNode *f[alphabetSize];
public:
trieNode()
{
for (uint tr = 0; tr < alphabetSize; ++tr)
f[tr] = 0;
}
void ins(char *c)
{
if (endWord(c))
{
++cnt;
return;
}
if (!f[*c - 'a'])
{
f[*c - 'a'] = new trieNode();
++oth;
}
f[*c - 'a']->ins(c+1);
}
int getCnt(char *c)
{
if (endWord(c))
return cnt;
if (!f[*c - 'a'])
return 0;
return f[*c-'a']->getCnt(c+1);
}
bool rem(char *c)
{
if (endWord(c))
{
--cnt;
if (!cnt && !oth) return true;
return false;
}
if (f[*c-'a']->rem(c+1))
{
delete f[*c-'a'];
f[*c-'a'] = 0;
--oth;
if (!cnt && !oth) return true;
}
return false;
}
int largestPref(char *c)
{
if (endWord(c) || !f[*c-'a'])
return 0;
return 1 + f[*c-'a']->largestPref(c+1);
}
void remAll()
{
if (!oth && !cnt)
return;
for (uint tr = 0; tr < alphabetSize; ++tr)
{
if (!f[tr])
continue;
f[tr]->remAll();
}
}
};
public:
trieNode *root;
trie()
{
root = new trieNode();
}
void addWordApp(char *w)
{
root->ins(w);
}
int countWordApp(char *w)
{
return root->getCnt(w);
}
void removeWordApp(char *w)
{
root->rem(w);
}
int getLargestWordPref(char *w)
{
return root->largestPref(w);
}
void clearTrie()
{
root->remAll();
}
};
int main()
{
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
trie t;
char s[lmax];
int op;
while(cin>>op>>s)
{
switch(op)
{
case 0:
t.addWordApp(s);
break;
case 1:
t.removeWordApp(s);
break;
case 2:
cout<<t.countWordApp(s)<<'\n';
break;
case 3:
cout<<t.getLargestWordPref(s)<<'\n';
break;
}
}
fclose(stdin);
fclose(stdout);
return 0;
}