Pagini recente » Cod sursa (job #441044) | Cod sursa (job #3192291) | Cod sursa (job #2439736) | Cod sursa (job #2267631) | Cod sursa (job #2916639)
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
const int N = 1e6 + 20;
int t[N][26], cnt[N], cnt_word[N], idx = 2;
void insert(string& s) {
int u = 1;
for(int i = 0; i < sz(s); ++i) {
if(t[u][s[i] - 'a'] == 0) {
t[u][s[i] - 'a'] = idx++;
}
u = t[u][s[i] - 'a'];
++cnt[u];
}
++cnt_word[u];
}
int count(string& s) {
int u = 1;
for(int i = 0; i < sz(s); ++i) {
if(!cnt[t[u][s[i] - 'a']]) return 0;
u = t[u][s[i] - 'a'];
}
return cnt_word[u];
}
int longest_prefix(string& s) {
int u = 1, ans = 0;
for(int i = 0; i < sz(s); ++i) {
if(!cnt[t[u][s[i] - 'a']]) return ans;
++ans;
u = t[u][s[i] - 'a'];
}
return ans;
}
void delete_string(string& s) {
int u = 1;
for(int i = 0; i < sz(s); ++i) {
u = t[u][s[i] - 'a'];
cnt[u]--;
}
--cnt_word[u];
}
void solve() {
freopen("trie.in", "r", stdin);
freopen("trie.out", "w", stdout);
int a; string b;
while(cin >> a) {
cin >> b;
if(a == 0) {
insert(b);
} else if(a == 1) {
delete_string(b);
} else if(a == 2) {
cout << count(b) << "\n";
} else {
cout << longest_prefix(b) << "\n";
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}