Pagini recente » jc2020/solutii/heist | Cod sursa (job #2238751) | Cod sursa (job #1383652) | Monitorul de evaluare | Cod sursa (job #1881243)
#include <bits/stdc++.h>
using namespace std;
struct nod{
int nr = 0, exact_nr = 0;
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_nr; }
void del(nod *n, const string& str){
--n->nr;
for(auto x : str)
(n = n->fii[x-'a']),
--n->nr;
--n->exact_nr; }
int nr_ap(nod *n, const string& str){
for(auto x : str){
if(!n->fii[x-'a']) return 0;
n = n->fii[x-'a']; }
return n->exact_nr; }
int long_common_pref(nod *n, const string& str){
int nr = 0;
while(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_common_pref(rad, str) << '\n'; }
return 0; }