Pagini recente » Cod sursa (job #890632) | Cod sursa (job #2623069) | Cod sursa (job #2634732) | Cod sursa (job #1996688) | Cod sursa (job #1181626)
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <list>
#include <bitset>
#include <ctime>
#include <cassert>
#include <iomanip>
using namespace std;
const string file = "trie";
const string inputF = file + ".in";
const string outputF = file + ".out";
const double epsilon = 1e-7;
#define LL long long
#define ULL unsigned long long
#define MOD1 666013
#define MOD2 666019
#define MOD3 1999999973
#define base 73
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> ii;
typedef pair<long long, long long> ll;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define all(V) V.begin(), V.end()
#define allr(V) V.rbegin(), V.rend()
#define for_c_it(container, it) for (auto it : container)
#define present(container, element) (container.find(element) != container.end())
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define zeroes(x) ((x ^ (x - 1)) & x)
class Trie {
public:
int nrw, nrson;
vector <Trie*> lit;
Trie() : nrw(0), nrson(0), lit(26, (Trie*) 0) {}
};
Trie *rootTrie;
void insert(Trie *root, char *s);
int erase(Trie *root, char *s);
int count(Trie *root, char *s);
int prefix(Trie *root, char *s, int lg);
int main() {
#ifndef INFOARENA
freopen("input.txt", "r", stdin);
#else
freopen(inputF.c_str(), "r", stdin);
freopen(outputF.c_str(), "w", stdout);
#endif
int cod;
char S[21];
rootTrie = new Trie();
rootTrie -> nrw = -1;
while (scanf("%d %s\n", &cod, S) > 0) {
if (cod == 0) {
insert(rootTrie, S);
}
else if (cod == 1) {
erase(rootTrie, S);
}
else if (cod == 2) {
printf("%d\n", count(rootTrie, S));
}
else {
printf("%d\n", prefix(rootTrie, S, 0));
}
}
return 0;
}
void insert(Trie *root, char *s) {
//cout << "insert " << s << '\n';
if (s[0] == '\0') {
root -> nrw++;
}
else {
int pos = s[0] - 'a';
if (root -> lit[pos] == 0) {
root -> lit[pos] = new Trie();
root -> nrson++;
}
insert(root -> lit[pos], s + 1);
}
}
int erase(Trie *root, char *s) {
//cout << "erase " << s << '\n';
if (s[0] == '\0') {
root -> nrw--;
}
else {
int pos = s[0] - 'a';
if (erase(root -> lit[pos], s + 1)) {
root -> lit[pos] = 0;
root -> nrson--;
}
}
if (root -> nrson == 0 && root -> nrw == 0) {
delete root;
return 1;
}
return 0;
}
int count(Trie *root, char *s) {
//cout << "count " << s << '\n';
if (s[0] == '\0') {
return root -> nrw;
}
if (root -> lit[s[0] - 'a'] != 0) {
return count(root->lit[s[0] - 'a'], s + 1);
}
return 0;
}
int prefix(Trie *root, char *s, int lg) {
//cout << "prefix " << s << '\n';
if (s[0] == '\0' || root -> lit[s[0] - 'a'] == 0 ) {
return lg;
}
return prefix(root -> lit[s[0] - 'a'], s + 1, lg + 1);
}