Pagini recente » Cod sursa (job #1381815) | Cod sursa (job #3030619) | Cod sursa (job #2357047) | Cod sursa (job #338317) | Cod sursa (job #2482398)
#include <fstream>
#include <map>
using namespace std;
ifstream cin("trie.in");
ofstream cout("trie.out");
struct node {
map< char, node > mp;
int signs = 0, exists = 0;
};
int main()
{
//cout << "Hello world!" << endl;
node root;
int op; string cnt;
while(cin >> op >> cnt) {
if(op == 0) {
node *self = &root;
for(int i = 0; i < cnt.size(); i++) {
self = &(self->mp[cnt[i]]);
self->exists++;
}
self->signs++;
}
if(op == 1) {
node *self = &root;
for(int i = 0; i < cnt.size(); i++) {
self = &(self->mp[cnt[i]]);
self->exists--;
}
self->signs--;
}
if(op == 2) {
node *self = &root;
for(int i = 0; i < cnt.size(); i++) {
self = &(self->mp[cnt[i]]);
}
cout << self->signs << endl;
}
if(op == 3) {
node *self = &root;
int len = 0;
for(; len < cnt.size() && (self->mp[cnt[len]]).exists; len++) {
if((self->mp[cnt[len]]).exists) {
self = &(self->mp[cnt[len]]);
}
}
cout << len << endl;
}
}
return 0;
}