Pagini recente » Cod sursa (job #1189421) | Cod sursa (job #2519162) | Cod sursa (job #1206155) | Cod sursa (job #398969) | Cod sursa (job #1881257)
#include <bits/stdc++.h>
using namespace std;
struct nod{
int nr, exact;
nod *fii[26]; };
void ins(nod *n, const string& str){
++n->nr;
for(const auto x : str){
if(!n->fii[x-'a']) n->fii[x-'a'] = new nod {};
n = n->fii[x-'a'];
++n->nr; }
++n->exact; }
void del(nod *n, const string& str){
--n->nr;
for(const auto x : str){
n = n->fii[x-'a'];
--n->nr; }
--n->exact; }
int nr_ap(nod *n, const string& str){
for(const auto x : str){
if(!n->fii[x-'a']) return 0;
n = n->fii[x-'a']; }
return n->exact; }
int long_pref(nod *n, const string& str){
int nr = 0;
while(nr < str.size() && n->fii[str[nr]-'a'] &&
n->fii[str[nr]-'a']->nr) n = n->fii[str[nr++]-'a'];
return nr; }
int main(){
ifstream f("trie.in");
ofstream g("trie.out");
nod *rad = new nod {};
int t;
string str;
while(f >> t){
f >> str;
if(t == 0) ins(rad, str);
else if(t == 1) del(rad, str);
else if(t == 2) g << nr_ap(rad, str) << '\n';
else g << long_pref(rad, str) << '\n'; }
return 0; }