Pagini recente » Cod sursa (job #2766543) | Cod sursa (job #605331) | Cod sursa (job #353977) | Cod sursa (job #431641) | Cod sursa (job #2410148)
#include <bits/stdc++.h>
using namespace std;
const int SIGMA = 26;
struct nod {
nod *fii[SIGMA];
int cnt = 0;
int cnt2 = 0;
nod () {
for(int i = 0; i < SIGMA; ++i) fii[i] = 0;
cnt = 0;
}
};
void modif(nod *n, string s, int i, int val) {
if(i == s.size()) {
n->cnt2 += val;
n->cnt += val;
return;
}
n->cnt += val;
if(!n->fii[s[i]-'a']) n->fii[s[i]-'a'] = new nod();
modif(n->fii[s[i]-'a'], s, i + 1, val);
}
int apar(nod *n, string s, int i) {
if(i == s.size()) {
return n->cnt2;
}
if(!n->fii[s[i]-'a']) return 0;
return apar(n->fii[s[i]-'a'], s, i + 1);
}
int lcp(nod *n, string s, int i) {
if(!n || !n->cnt) return 0;
if(n->fii[s[i]-'a'] && n->fii[s[i]-'a']->cnt != 0) return 1 + lcp(n->fii[s[i]-'a'], s, i + 1);
return 0;
}
int main() {
#ifdef BLAT
freopen("input", "r", stdin);
#else
freopen("trie.in", "r", stdin);
freopen("trie.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
nod* root = new nod();
int t;
while(cin >> t) {
string s;
cin >> s;
if(t == 0) {
modif(root, s, 0, 1);
}
if(t == 1) {
modif(root, s, 0, -1);
}
if(t == 2) {
cout << apar(root, s, 0) << '\n';
}
if(t == 3) {
cout << lcp(root, s, 0) << '\n';
}
}
return 0;
}