Pagini recente » Cod sursa (job #507836) | Cod sursa (job #214705) | Cod sursa (job #1641013) | Cod sursa (job #683138) | Cod sursa (job #970808)
Cod sursa(job #970808)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct nod {
int val_aps,val_path,A[28];
};
#include <vector>
#include <string>
string str;
vector<nod> Trie;
int pos,N,nr_nodes,n;
#define pb push_back
nod nul_node;
void update(int semn,int node) {
Trie[node].val_path+=semn;
++pos;
if (pos==N) {
Trie[node].val_aps+=semn;
return;
}
if (Trie[node].A[str[pos]-'a']==0) {
Trie[node].A[str[pos]-'a']=++nr_nodes;
Trie.pb(nul_node);
}
update(semn,Trie[node].A[str[pos]-'a']);
}
int query_one(int node) {
++pos;
if (pos==N) return Trie[node].val_aps;
if (Trie[node].A[str[pos]-'a']==0)
return 0;
else
return query_one(Trie[node].A[str[pos]-'a']);
}
int query_two(int node) {
++pos;
if (pos==N) return N;
if (Trie[node].A[str[pos]-'a']==0||Trie[Trie[node].A[str[pos]-'a']].val_path==0) return pos;
return query_two(Trie[node].A[str[pos]-'a']);
}
int main() {
Trie.pb(nul_node);
Trie.pb(nul_node);
int typ;
for(int i=1; f>>typ; ++i) {
f>>str;
N=str.length();
pos=-1;
if (typ==0) update(1,1);
if (typ==1) update(-1,1);
if (typ==2) g<<query_one(1)<<'\n';
if (typ==3) g<<query_two(1)<<'\n';
}
f.close();
g.close();
return 0;
}