Pagini recente » Cod sursa (job #1323285) | Cod sursa (job #833883) | Cod sursa (job #2309648) | Cod sursa (job #2252287) | Cod sursa (job #2665129)
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < int , int >
#define pq priority_queue
#define dbg(x) cerr << #x << ": " << x << '\n'
using namespace std;
const int N = 1e5 + 10;
const int M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ifstream f ("trie.in");
ofstream g ("trie.out");
struct Trie
{
Trie *alp[200];
int cnt , let;
Trie()
{
cnt = let = 0;
memset(alp , 0 , sizeof(alp));
}
};
Trie *T = new Trie;
char line[30];
void add(Trie *&nod , char *ch)
{
if(*ch == '\0')
return ++nod -> cnt , void();
if(nod -> alp[*ch] == NULL)
++nod -> let , nod -> alp[*ch] = new Trie;
add(nod -> alp[*ch] , ch + 1);
}
bool del(Trie *&nod , char *ch)
{
if(*ch == '\0')
{
--nod -> cnt;
if(!nod -> cnt && !nod -> let && nod != T)
return delete nod , 1;
return 0;
}
bool ok = del(nod -> alp[*ch] , ch + 1);
if(ok)
{
nod -> alp[*ch] = NULL;
--nod -> let;
if(!nod -> let && nod != T)
return delete nod , 1;
}
return 0;
}
void cnt(Trie *nod , char *ch)
{
if(*ch == '\0')
return g << nod -> cnt << '\n' , void();
if(nod -> alp[*ch] == NULL)
return g << 0 << '\n' , void();
cnt(nod -> alp[*ch] , ch + 1);
}
void pref(Trie *nod , char *ch , int ans)
{
if(*ch == '\0')
return g << ans << '\n' , void();
if(nod -> alp[*ch] == NULL)
return g << ans << '\n' , void();
pref(nod -> alp[*ch] , ch + 1 , ans + 1);
}
signed main()
{
#ifndef ONLINE_JUDGE
// freopen("trie.in" , "r" , stdin) , freopen("trie.out" , "w" , stdout);
ios_base::sync_with_stdio(false) , f.tie(0) , cout.tie(0);
#endif
while(f.getline(line , 20))
{
if(*line == '0')
add(T , line + 2);
else if(*line == '1')
del(T , line + 2);
else if(*line == '2')
cnt(T , line + 2);
else if(*line == '3')
pref(T , line + 2 , 0);
}
return 0;
}