Pagini recente » Cod sursa (job #1098875) | Cod sursa (job #205689) | Cod sursa (job #1231367) | Cod sursa (job #3163892) | Cod sursa (job #2750925)
#include <fstream>
#include <string>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
typedef string::iterator si;
const int sigma=26;
struct node{
int x;
node *v[sigma];
node();
void add(si p);
void remove(si p);
int get(si p);
int prefix(si p);
};
node::node(){
x=1;
for(int i=0;i<sigma;i++){
v[i]=nullptr;
}
}
void node::add(si p){
if((*p)!=0){
int c=(*p)-'a';
if(v[c]==nullptr){
v[c]=new node;
}else{
v[c]->x++;
}
v[c]->add(p+1);
}
}
void node::remove(si p){
if((*p)!=0){
int c=(*p)-'a';
v[c]->remove(p+1);
v[c]->x--;
}
}
int node::get(si p){
int sol=0;
if((*p)!=0){
int c=(*p)-'a';
if(v[c]!=nullptr){
sol=v[c]->get(p+1);
}
}else{
sol=x;
for(int i=0;i<sigma;i++){
if(v[i]!=nullptr){
sol-=v[i]->x;
}
}
}
return sol;
}
int node::prefix(si p){
if((*p)!=0){
int c=(*p)-'a';
if(v[c]!=nullptr&&v[c]->x>0){
return v[c]->prefix(p+1)+1;
}else{
return 0;
}
}else{
return 0;
}
}
int main(){
int x;
string s;
node *trie=new node;
while(fin>>x>>s){
if(x==0){
trie->add(s.begin());
}else if(x==1){
trie->remove(s.begin());
}else if(x==2){
fout<<trie->get(s.begin())<<"\n";
}else{
fout<<trie->prefix(s.begin())<<"\n";
}
}
return 0;
}