Pagini recente » Cod sursa (job #2322560) | Cod sursa (job #1502162) | Cod sursa (job #373677) | Cod sursa (job #2988013) | Cod sursa (job #2664352)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
string s;
struct trienod{
int cnt, depth;
trienod *v[27];
trienod(int d){
cnt=0, depth=d;
for(int i=0;i<=26;++i) v[i]=nullptr;
}
void add(){
cnt++;
if(depth==s.size()) return;
char x;
if(depth+1==s.size()) x=0;
else x=s[depth+1]-'a'+1;
if(v[x]==nullptr) v[x]=new trienod(depth+1);
v[x]->add();
}
bool del(){
cnt--;
if(depth==s.size()) return !cnt;
char x;
if(depth+1==s.size()) x=0;
else x=s[depth+1]-'a'+1;
if(v[x]->del()){
delete v[x];
v[x]=nullptr;
}
return !cnt;
}
int srcf(){
if(depth==s.size()) return cnt;
char x;
if(depth+1==s.size()) x=0;
else x=s[depth+1]-'a'+1;
if(v[x]==nullptr) return 0;
return v[x]->srcf();
}
int srcp(){
if(depth+1==s.size()) return depth;
char x=s[depth+1]-'a'+1;
if(v[x]==nullptr) return depth;
return v[x]->srcp();
}
};
int main(){
int tip;
trienod x(-1);
while(fin>>tip){
if(tip==EOF) return 0;
fin>>s;
if(tip==0) x.add();
else if(tip==1) x.del();
else if(tip==2) fout<<x.srcf()<<"\n";
else fout<<x.srcp()+1<<"\n";
}
return 0;
}