Pagini recente » Cod sursa (job #2330827) | Cod sursa (job #3197733) | Cod sursa (job #3200476) | Cod sursa (job #2625099) | Cod sursa (job #2691083)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
class Trie {
private:
int cnt = 0;
int lvs = 0;
Trie *next[26] = {};
public:
void insert(const string& str, int pos = 0) {
lvs++;
if (pos == (int) str.size())
cnt++;
else {
if (!next[str[pos] - 'a'])
next[str[pos] - 'a'] = new Trie;
next[str[pos] - 'a']->insert(str, pos + 1);
}
}
bool erase(const string& str, int pos = 0) {
if (pos == (int) str.size()) {
if (lvs > 1) {
cnt--;
lvs--;
return false;
}
return true;
}
if (!next[str[pos] - 'a']->erase(str, pos + 1)) {
lvs--;
return false;
}
delete next[str[pos] - 'a'];
next[str[pos] - 'a'] = nullptr;
return --lvs == 0;
}
int count(const string& str, int pos = 0) {
if (pos == (int) str.size())
return cnt;
if (!next[str[pos] - 'a'])
return 0;
return next[str[pos] - 'a']->count(str, pos + 1);
}
int lcp(const string& str, int pos = 0) {
if (pos == (int) str.size())
return 0;
if (!next[str[pos] - 'a'])
return 0;
return 1 + next[str[pos] - 'a']->lcp(str, pos + 1);
}
};
int main() {
Trie trie;
int type; string str;
while (fin >> type >> str)
if (!type)
trie.insert(str);
else if (type == 1)
trie.erase(str);
else if (type == 2)
fout << trie.count(str) << '\n';
else
fout << trie.lcp(str) << '\n';
return 0;
}