Pagini recente » Cod sursa (job #1165310) | Cod sursa (job #2348093) | Cod sursa (job #1549188) | Rating Pal Juhasz (bontovicspal) | Cod sursa (job #1290462)
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "trie";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME
typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;
const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = (int)(1e9) + 7;
const int NMAX = 100000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;
struct node {
int cnt, wds;
node *f[26];
node() {
cnt = wds = 0;
memset(f, 0, sizeof(f));
}
};
node *root;
void insert(char *p) {
int c;
node *Q = root;
for(; *p; p++) {
c = *p - 'a';
if(!Q->f[c])
Q->f[c] = new node;
Q = Q->f[c];
Q->cnt++;
}
Q->wds++;
}
void erase(char *w) {
int c;
node *Q = root;
char *p = w;
for(; *p; p++) {
c = *p - 'a';
Q = Q->f[c];
Q->cnt--;
}
Q->wds--;
}
int count(char *p) {
int c;
node *Q = root;
for(; *p; p++) {
c = *p - 'a';
if(!Q->f[c] || !Q->f[c]->cnt)
return 0;
Q = Q->f[c];
}
return Q->wds;
}
int prefix(char *p) {
int c, ans = 0;
node *Q = root;
for(; *p; p++, ans++) {
c = *p - 'a';
if(!Q->f[c] || !Q->f[c]->cnt)
return ans;
Q = Q->f[c];
}
return ans;
}
int main() {
int t;
char w[25];
#ifndef ONLINE_JUDGE
freopen(inputFile.c_str(), "r", stdin);
freopen(outputFile.c_str(), "w", stdout);
#endif
root = new node;
while(scanf("%d %s\n", &t, w) + 1) {
if(t == 0)
insert(w);
if(t == 1)
erase(w);
if(t == 2)
printf("%d\n", count(w));
if(t == 3)
printf("%d\n", prefix(w));
}
return 0;
}