Pagini recente » Cod sursa (job #2426220) | Cod sursa (job #2546241) | Cod sursa (job #419014) | Cod sursa (job #490587) | Cod sursa (job #3184815)
/**
* Author: Andu Scheusan (not_andu)
* Created: 16.12.2023 21:45:43
*/
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define INFILE "trie.in"
#define OUTFILE "trie.out"
typedef long long ll;
class Trie{
private:
int cnt = 0;
int level = 0;
Trie *children[26] = {};
public:
void insert(string s, int position = 0){
++level;
if(s.size() == position) ++cnt;
else{
int to = s[position] - 'a';
if(!children[to]) children[to] = new Trie;
children[to] -> insert(s, position + 1);
}
}
void erase(string s, int position = 0){
--level;
if(s.size() == position) --cnt;
else{
int to = s[position] - 'a';
children[to] -> erase(s, position + 1);
if(!children[to] -> level){
delete children[to];
children[to] = nullptr;
}
}
}
int count(string s, int poisition = 0){
if(poisition == s.size()) return cnt;
int to = s[poisition] - 'a';
if(!children[to]) return 0;
return children[to] -> count(s, poisition + 1);
}
int longestCommonPrefix(string s, int position = 0){
if(position == s.size()) return 0;
int to = s[position] - 'a';
if(!children[to]) return 0;
return 1 + children[to] -> longestCommonPrefix(s, position + 1);
}
};
void solve(){
Trie trie;
int type;
string word;
while(cin >> type >> word){
if(type == 0){
trie.insert(word);
}
else if(type == 1){
trie.erase(word);
}
else if(type == 2){
cout << trie.count(word) << '\n';
}
else{
cout << trie.longestCommonPrefix(word) << '\n';
}
}
}
int main(){
ios_base::sync_with_stdio(false);
freopen(INFILE, "r", stdin);
freopen(OUTFILE, "w", stdout);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}