Pagini recente » Cod sursa (job #2651320) | Cod sursa (job #1360606) | Cod sursa (job #1140172) | Cod sursa (job #528255) | Cod sursa (job #2082636)
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct trie {
int nr = 0;
trie *kov[26] = { 0 };
} *fa = new trie();
int main()
{
ifstream f;
ofstream g;
f.open("trie.in");
g.open("trie.out");
char c[20]; int n;
while(f >> n >> c) {
trie *temp_fa = fa;
int len = strlen(c), tmp;
if(n == 0) {
for(int i = 0; i < len; ++i) {
tmp = c[i] - 'a';
if(!temp_fa->kov[tmp]) {
temp_fa->kov[tmp] = new trie;
}
temp_fa = temp_fa -> kov[tmp];
}
temp_fa->nr += 1;
} else if(n == 1) {
for(int i = 0; i < len; ++i) {
tmp = c[i] - 'a';
temp_fa = temp_fa -> kov[tmp];
}
temp_fa->nr =- 1;
} else if(n == 2) {
for(int i = 0; i < len; ++i) {
tmp = c[i] - 'a';
temp_fa = temp_fa -> kov[tmp];
}
g << temp_fa->nr << "\n";
} else if(n == 3) {
int prefix = 0;
for(int i = 0; i < len; ++i) {
tmp = c[i] - 'a';
if(!temp_fa->nr) { prefix = i; }
if(!temp_fa -> kov[tmp]) {
break;
}
temp_fa = temp_fa -> kov[tmp];
}
g << prefix << "\n";
}
}
f.close();
return 0;
}