Pagini recente » Cod sursa (job #1099841) | Cod sursa (job #1435502) | Cod sursa (job #2791690) | Borderou de evaluare (job #434174) | Cod sursa (job #3324776)
#include <fstream>
#include <vector>
#include <map>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
ifstream cin("trie.in");
ofstream cout("trie.out");
#define ll long long
const int SIGMA = 26;
struct nod{
nod* pr[SIGMA];
int subsize = 0;
int cnt;
nod()
{
cnt = 0;
for(int i = 0; i < SIGMA; i++)
pr[i] = NULL;
}
void add(char ch)
{
if(pr[ch - 'a'] == NULL)
pr[ch - 'a'] = new nod();
}
};
class TRIE{
nod* root = new nod();
void add(nod* p, string& s, int poz, int val)
{
p -> subsize += val;
if(poz == s.size())
{
p -> cnt += val;
return;
}
p -> add(s[poz]);
add(p -> pr[s[poz] - 'a'], s, poz + 1, val);
}
int getfr(nod* p, string& s, int poz)
{
if(poz == s.size())
return p -> cnt;
if(!(p -> pr[s[poz] - 'a']))
return 0;
return getfr(p -> pr[s[poz] - 'a'], s, poz + 1);
}
int maxpref(nod* p, string& s, int poz)
{
if(poz == s.size())
return poz;
if(!(p -> pr[s[poz] - 'a']) || !(p -> pr[s[poz] - 'a'] -> subsize))
return poz;
return maxpref(p -> pr[s[poz] - 'a'], s, poz + 1);
}
public:
void add(string& s){add(root, s, 0, 1);}
void del(string& s){add(root, s, 0, -1);}
int getfr(string& s){return getfr(root, s, 0);}
int maxpref(string &s){return maxpref(root, s, 0);}
};
TRIE copac;
void processQuery()
{
int tip; string s;
while(cin>>tip)
{
cin>>s;
if(tip == 0)
copac.add(s);
if(tip == 1)
copac.del(s);
if(tip == 2)
cout<<copac.getfr(s)<<'\n';
if(tip == 3)
cout<<copac.maxpref(s)<<'\n';
}
}
int main()
{
//ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
processQuery();
return 0;
}