Pagini recente » Cod sursa (job #79339) | Cod sursa (job #1203173) | Cod sursa (job #2708544) | Cod sursa (job #1254599) | Cod sursa (job #3176810)
//#include <bits/stdc++.h>
//
//
//using namespace std;
//
//const int k=26;
//struct Vertex
//{
// int next[k];
// int words;
// int prefixes;
// Vertex(){
// fill(begin(next),end(next),-1);
// }
//};
//vector<Vertex> trie(1);
//
//void addWord(const string& s){
// int v=0;
// for(char ch:s){
// int c=ch-'a';
// if(trie[v].next[c]==-1){
// trie[v].next[c]=trie.size();
// trie.emplace_back();
// }
//
// v=trie[v].next[c];
// trie[v].prefixes++;
// }
// trie[v].words++;
//
//}
//void removeWord(const string &s){
// vector<int> nodes;
// int v=0;
// for(char ch:s){
// int c=ch-'a';
// if(trie[v].next[c]==-1){
// printf("Word not found");
// return;
// }
// nodes.push_back(v);
// v=trie[v].next[c];
// }
// if(trie[v].words)
// trie[v].words--;
// else
// trie[v].prefixes--;
////
//// if(trie[v].words==0 && trie[v].prefixes==0)
//// for(int i=nodes.size()-1;i>=0;--i){
//// int c=s[i]-'a';
//// int u=nodes[i];
//// if (trie[u].words > 0 || trie[u].prefixes > 0) break;
//// trie[u].next[c] = -1;
//// }
//
// ///daca este frunza stergem nodul
// if(trie[v].words==0&&trie[v].prefixes==0){
//
// }
//
//
// int i=nodes.size()-1;
// while(i && trie[v].words==0 && trie[v].prefixes==0){
// int c=s[i]-'a';
// trie[v].next[c]=-1;
// v=nodes[i-1];
// i--;
// }
//}
//int countWords(const string &s) {
// int v=0;
// for(char ch:s){
// int c=ch-'a';
// if(trie[v].next[c]==-1)
// return 0;
// v=trie[v].next[c];
// }
// return trie[v].words;
//}
//int longestPrefix(const string &s){
// int v=0;
// int lmax=0;
// for(char ch:s){
// int c=ch-'a';
// if(trie[v].next[c]==-1)
// break;
// lmax++;
// v=trie[v].next[c];
// }
// return lmax;
//}
//void printTrie(){
// printf("Printing trie:\n");
// for(int i=0;i<trie.size();i++){
// for(int c=0;c<26;c++)
// if(trie[i].next[c]>0)
// printf("vertex=%d, ch=%c\n",i,((char)(c+'a')));
// }
//}
//
//int main()
//{
// freopen("trie.in","r",stdin);
//// freopen("dictree.out","w",stdout);
// int n,op;
// string s;
//
// while(cin>>op>>s){
//
// if(op==0){
// addWord(s);
// }else if(op==1){
// removeWord(s);
// }else if(op==2){
// printf("%d\n",countWords(s));
// }else{
// printf("%d\n",longestPrefix(s));
// }
// printTrie();
// }
//
// return 0;
//}
#include <bits/stdc++.h>
using namespace std;
class Trie{
int words=0;
int prefixes=0;
Trie *next[26]={};
public:
void insertStr(const string &s, int pos=0){
if(pos==int(s.size()))
words++;
else{
int c=s[pos]-'a';
if(!next[c])
next[c]=new Trie;
next[c]->prefixes++;
next[c]->insertStr(s,pos+1);
}
}
int countStr(const string &s,int pos=0){
if(pos==int(s.size()))
return words;
else{
int c=s[pos]-'a';
if(!next[c])
return 0;
return next[c]->countStr(s,pos+1);
}
}
int lcp(const string &s,int pos=0){
if(pos==int(s.size()))
return 0;
int c=s[pos]-'a';
if(!next[c])
return 0;
return 1+next[c]->lcp(s,pos+1);
}
void eraseStr(const string &s, int pos=0){
prefixes--;
if(pos==int(s.size()))
words--;
else{
int c=s[pos]-'a';
next[c]->eraseStr(s,pos+1);
if(next[c]->prefixes==0){
delete next[c];
next[c]=nullptr;
}
}
}
};
using namespace std;
int main(){
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
Trie *trie=new Trie;
int n,op;
string s;
while(cin>>op>>s){
if(op==0){
trie->insertStr(s);
}else if(op==1){
trie->eraseStr(s);
}else if(op==2){
printf("%d\n",trie->countStr(s));
}else{
printf("%d\n",trie->lcp(s));
}
}
return 0;
}