Pagini recente » Cod sursa (job #2915950) | Cod sursa (job #2964337) | Cod sursa (job #1042655) | Cod sursa (job #2207515) | Cod sursa (job #937337)
Cod sursa(job #937337)
#include <fstream>
#include <map>
using namespace std;
struct trie
{
int cnt;
map<char,trie*> child;
trie() : cnt(0){}
};
int main() {
ifstream fin("trie.in");
ofstream fout("trie.out");
trie *tmp, *tmp1, *root = new trie;
root->cnt = 1;
map<char,trie*>::iterator it;
int q;
char c;
while (fin >> q) {
fin.get(c);
tmp = root;
if (q == 0) {
while (c != '\n') {
fin.get(c);
it = tmp->child.find(c);
if (it != tmp->child.end()) tmp = it->second;
else tmp = tmp->child[c] = new trie;
++tmp->cnt;
}
}
else if (q == 1) {
while (c != '\n') {
fin.get(c);
tmp1 = tmp->child[c];
if (tmp1->cnt == 1) tmp->child.erase(c);
if (tmp->cnt == 0) delete tmp;
tmp = tmp1;
--tmp->cnt;
}
if (tmp->cnt == 0) delete tmp;
}
else if (q == 2) {
while (c != '\n') {
fin.get(c);
it = tmp->child.find(c);
if (it == tmp->child.end()) break;
tmp = it->second;
}
while (c != '\n') fin.get(c);
if (tmp->child.empty()) fout << tmp->cnt << '\n';
else fout << 0 << '\n';
}
else {
int ans = 0;
while (c != '\n') {
fin.get(c);
it = tmp->child.find(c);
if (it == tmp->child.end()) break;
++ans;
tmp = it->second;
}
while (c != '\n') fin.get(c);
if (tmp->child.empty()) --ans;
fout << ans << '\n';
}
}
return 0;
}