Pagini recente » Cod sursa (job #1087600) | Cod sursa (job #1055242) | Cod sursa (job #735157) | Cod sursa (job #1237700) | Cod sursa (job #1986956)
#include <bits/stdc++.h>
using namespace std;
struct nod{
nod *fii[26] = {};
int nr_ap = 0, nr_ap_pref = 0; };
void add_str(nod *n, const string& str){
++n->nr_ap_pref;
for(const auto x : str){
if(!n->fii[x-'a']) n->fii[x-'a'] = new nod {};
n = n->fii[x-'a'];
++n->nr_ap_pref; }
++n->nr_ap; }
void rem_str(nod *n, const string& str){
--n->nr_ap_pref;
for(const auto x : str){
n = n->fii[x-'a'];
--n->nr_ap_pref; }
--n->nr_ap; }
nod *get_nod(nod *n, const string& str){
for(const auto x : str)
n = n->fii[x-'a'];
return n;
}
int get_common_pref_node_depth(nod *n, const string& str){
int rez = 0;
for(const auto x : str){
if(n->fii[x-'a'] && n->fii[x-'a']->nr_ap_pref){
n = n->fii[x-'a'];
++rez; }
else return rez; }
return rez;
}
ifstream f("trie.in");
ofstream g("trie.out");
nod *n = new nod;
void op_0(const string& str){
add_str(n, str); }
void op_1(const string& str){
rem_str(n, str);
}
void op_2(const string& str){
add_str(n, str);
rem_str(n, str);
g << get_nod(n, str)->nr_ap << '\n';
}
void op_3(const string& str){
add_str(n, str);
rem_str(n, str);
g << get_common_pref_node_depth(n, str) << '\n';
}
int main()
{
int x;
string s;
while(f>>x>>s){
if(x==0) op_0(s);
if(x==1) op_1(s);
if(x==2) op_2(s);
if(x==3) op_3(s);
}
return 0;
}