Pagini recente » Cod sursa (job #1394257) | Cod sursa (job #217808) | Cod sursa (job #683930) | Cod sursa (job #2797146) | Cod sursa (job #2750265)
#include<bits/stdc++.h>
#include<queue>
#include<stack>
#define INFILE fin("trie.in");
#define OUTFILE fout("trie.out");
using namespace std;
ifstream INFILE
ofstream OUTFILE
class Trie{
private:
int cnt = 0;
int lvs = 0;
Trie *next[26] = {};
public:
void insert(const string& str, int pos = 0)
{
lvs++;
if(pos == (int)str.size())
{
cnt++;
}
else{
if(!next[str[pos] - 'a'])
{
next[str[pos] - 'a'] = new Trie;
}
next[str[pos] - 'a']->insert(str,pos+1);
}
}
void erase(const string& str,int pos = 0)
{
lvs--;
if(pos==(int)str.size())
cnt--;
else{
next[str[pos] - 'a']->erase(str,pos+1);
if(!next[str[pos] - 'a']->lvs)
{
delete next[str[pos] - 'a'];
next[str[pos] - 'a'] = nullptr;
}
}
}
int count(const string& str, int pos = 0)
{
if(pos == (int)str.size())
{
return cnt;
}
if(!next[str[pos] - 'a'])
return 0;
return next[str[pos] - 'a']->count(str,pos+1);
}
int lcp(const string& str,int pos = 0)
{
if(pos == (int)str.size())
return 0;
if(!next[str[pos] - 'a'])
return 0;
return 1+next[str[pos] - 'a']->lcp(str,pos+1);
}
};
int main()
{
Trie tr;
int t;
string sir;
while(fin>>t>>sir)
{
if(!t)
tr.insert(sir);
else if(t == 1)
tr.erase(sir);
else if(t == 2)
fout<<tr.count(sir)<<'\n';
else fout<<tr.lcp(sir)<<'\n';
}
return 0;
}