Pagini recente » Cod sursa (job #636951) | Cod sursa (job #1872760) | Cod sursa (job #86724) | Cod sursa (job #652198) | Cod sursa (job #1377163)
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
struct trie
{
trie *f[26];
int was, cnt;
trie()
{
memset(f, 0, sizeof(f));
was = cnt = 0;
}
};
trie *root = new trie;
int op, n, l;
char s[25];
void add(trie *t, int poz)
{
if(poz == n)
{
t->cnt++;
return;
}
int l = s[poz] - 'a';
if(!t->f[l]) t->f[l] = new trie;
t->f[l]->was++;
add(t->f[l], poz + 1);
}
void erase(trie *t, int poz)
{
if(poz == n)
{
t->cnt--;
return;
}
int l = s[poz] - 'a';
t->f[l]->was--;
erase(t->f[l], poz + 1);
}
void count(trie *t, int poz)
{
if(poz == n)
{
printf("%d\n", t->cnt);
return;
}
int l = s[poz] - 'a';
if(!t->f[l] || !t->f[l]->was)
{
printf("0\n");
return;
}
count(t->f[l], poz + 1);
}
void prefix(trie *t, int poz)
{
if(poz == n)
{
printf("%d\n", n);
return;
}
int l = s[poz] - 'a';
if(!t->f[l] || !t->f[l]->was)
{
printf("%d\n", poz);
return;
}
prefix(t->f[l], poz + 1);
}
int main()
{
freopen("trie.in", "r", stdin);
freopen("trie.out", "w", stdout);
while(scanf("%d %s\n", &op, s) + 1)
{
n = strlen(s);
if(op == 0) add(root, 0);
else if(op == 1) erase(root, 0);
else if(op == 2) count(root, 0);
else prefix(root, 0);
}
return 0;
}