Pagini recente » Cod sursa (job #1044054) | Cod sursa (job #1915677) | Cod sursa (job #2299455) | Cod sursa (job #821128) | Cod sursa (job #3257409)
#include <bits/stdc++.h>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
int n , x , nr;
char c[105];
struct Node
{
int cnt;
int nrc;
Node *son[26];
Node(){
cnt=0;
nrc=0;
for(int i=0;i<26;i++) son[i]=nullptr;
}
};
Node *t=new Node();
void update1(Node *nod , char *p , int l);
bool update2(Node *nod , char *p , int l);
int query1(Node *nod , char *p , int l);
int query2(Node *nod , char *p , int l , int n);
int main()
{
while(f >> x){
f.get();
f.getline(c , 26);
int n=strlen(c);
if(x==0){
update1(t , c , n);
}
else if(x==1){
update2(t , c , n);
}
else if(x==2){
g << query1(t , c , n) << '\n';
}
else if(x==3){
g << query2(t , c , n , n) << '\n';
}
}
}
void update1(Node *nod , char *p , int l)
{
if(l==0){
++nod->cnt;
return;
}
int c=*p-'a';
if(nod->son[c]==nullptr) {
++nod->nrc;
nod->son[c]=new Node();
}
update1(nod->son[c] , p+1 , l-1);
}
bool update2(Node *nod , char *p , int l)
{
if(l==0){
--nod->cnt;
if(nod->cnt==0 && nod->nrc==0){
delete nod;
return true;
}
return false;
}
int c=*p-'a';
if(update2(nod->son[c] , p+1 , l-1)==true){
nod->son[c]=nullptr;
nod->nrc--;
}
if(nod->cnt==0 && nod->nrc==0){
delete nod;
return true;
}
return false;
}
int query1(Node *nod , char *p , int l)
{
int c=*p-'a';
if(l==0) return nod->cnt;
if(nod->son[c]==nullptr) return 0;
query1(nod->son[c] , p+1 , l-1);
}
int query2(Node *nod , char *p , int l , int n)
{
int c=*p-'a';
if(l==0) return n-l;
if(nod->son[c]==nullptr) return n-l;
query2(nod->son[c] , p+1 , l-1 , n);
}