Pagini recente » Cod sursa (job #87529) | Cod sursa (job #1812950) | Cod sursa (job #2875437) | Cod sursa (job #2402250) | Cod sursa (job #2308247)
//#include <bits/stdc++.h>
#include <fstream>
#include <vector>
#include <bitset>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <math.h>
#include <iomanip>
#include <stack>
#include <string.h>
#include <set>
using namespace std;
ifstream in("trie.in");
ofstream out("trie.out");
const int sigma = 30;
const int DIM = 1e5 + 17;
int to[DIM][sigma];
int k = 0;
int apps [DIM * sigma];
int prefix [DIM * sigma];
void add(string word)
{
int nod = 0;
for(int i = 0; i < word.size(); i++)
{
if(to[nod][word[i] - 'a'] == 0)
to[nod][word[i] - 'a'] = ++k;
nod = to[nod][word[i] - 'a'];
prefix[nod]++;
}
apps[nod]++;
}
void del(string word)
{
int nod = 0;
for(int i = 0; i < word.size(); i++)
{
nod = to[nod][word[i] - 'a'];
prefix[nod]--;
}
apps[nod]--;
}
void printApps(string word)
{
int nod = 0;
for(int i = 0; i < word.size(); i++)
{
nod = to[nod][word[i] - 'a'];
if(!prefix[nod])
{
out << 0 << '\n';
return ;
}
}
out << apps[nod] << '\n';
}
void printLen(string word)
{
int nod = 0;
for(int i = 0; i < word.size(); i++)
{
nod = to[nod][word[i] - 'a'];
if(!prefix[nod])
{
out << i << '\n';
return ;
}
}
out << word.size() << '\n';
}
int main()
{
int op;
string word;
while(in >> op >> word)
{
switch(op)
{
case(0) : add(word); break;
case(1) : del(word); break;
case(2) : printApps(word); break;
case(3) : printLen(word); break;
}
}
}