Pagini recente » Statistici popa diana cristina (dianacristina415) | Cod sursa (job #2798512) | Cod sursa (job #261428) | Cod sursa (job #1327894) | Cod sursa (job #2642400)
#include<bits/stdc++.h>
using namespace std;
int x;
string s;
class TrieNode {
map<char, TrieNode*> children;
int cnt;
int ending;
public:
TrieNode() {
cnt = 0;
ending = 0;
}
void insert(string &s, int ind) {
++cnt;
if(ind == s.size()) {
++ending;
return;
}
if(!children[s[ind]]) children[s[ind]] = new TrieNode();
children[s[ind]]->insert(s, ind+1);
}
void del(string &s, int ind) {
--cnt;
if(ind == s.size()) {
--ending;
return;
}
children[s[ind]]->del(s, ind+1);
}
int count(string &s, int ind) {
if(ind == s.size()) {
return ending;
}
if(!children[s[ind]]) return 0;
return children[s[ind]]->count(s, ind+1);
}
int lp(string &s, int ind) {
if(!cnt) return max(0, ind-1);
if(ind == s.size() || !children[s[ind]]) {
return ind;
}
return children[s[ind]]->lp(s, ind+1);
}
};
int main() {
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
TrieNode* root = new TrieNode();
while(cin>>x>>s) {
if(x == 0) root->insert(s, 0);
if(x == 1) root->del(s, 0);
if(x == 2) cout<<root->count(s, 0)<<"\n";
if(x == 3) cout<<root->lp(s, 0)<<"\n";
}
}